C# WPF鼠标悬停在按钮上时如何更改背景图像
我已经写了这个代码插入到一个按钮的文本背景图像。所以我有这个:C# WPF鼠标悬停在按钮上时如何更改背景图像,c#,wpf,xaml,button,background-image,C#,Wpf,Xaml,Button,Background Image,我已经写了这个代码插入到一个按钮的文本背景图像。所以我有这个: <Button Style="{StaticResource TransparentButton}" Grid.Column="0" Grid.Row="0" Command="{Binding Path=MovePreviousCommand}" Width="150"> <Button.Content> <Sta
<Button Style="{StaticResource TransparentButton}"
Grid.Column="0"
Grid.Row="0"
Command="{Binding Path=MovePreviousCommand}"
Width="150">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Grid>
<Image Source="./Resources/Images/bottone_indietro.png" Width="130"/>
<Label Content="{x:Static res:Strings.GestoreWizardView_Button_MovePrevious}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="48,10,26,8" Style="{StaticResource LabelStyleButton}"
/>
</Grid>
</StackPanel>
</Button.Content>
</Button>
这是一个样式按钮
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在我想改变背景图像时,鼠标是在按钮
我们能帮我吗
最佳规范。如果您想要的图像位于资源文件夹中,则此功能应该可以正常工作
<Style x:Key="TransparentButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="0"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="Resources/image.jpg" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
要使其紧凑:
<Image Width="130" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="./Resources/Images/bottone_indietro.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
<Setter Property="Source" Value="./Resources/Images/bottone_indietro_altro.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
我得到了一个无害的设计时异常(运行良好),因为按钮作为祖先仅在运行时检测到。如果这是一个问题,您可以尝试。是按钮的背景还是整个窗口的背景?是按钮的背景吗?我尝试使用您的代码,但背景图像在默认背景图像下工作,我认为在按钮上插入背景图像的方法是错误的“”确保您的图像将其生成操作设置为resource。这里的问题是您在将原始图像添加为内容时设置了按钮背景。它需要匹配,无论是作为背景还是作为内容。谢谢,我已经解决了您的代码的问题,我更改了一个背景图像按钮,如下所示: