The following code effectively displays an image as the background for a button and changes the image on a isMouseOver
event. What I would like to be able to do is use the same style for multiple buttons but be able to change the background image (ImageBrush
) individually for each button since each one will use a different image.
Is it possible to use the same Resource Style in multiple buttons and change the background image individually on an IsMouseOver
or do I need separate styles for each one?
Style
<Window.Resources>
<Style x:Key="MainMenuButtonTemplate" TargetType="{x:Type Button}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="button" CornerRadius="12"
Background="{TemplateBinding Background}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<TextBlock Text="{TemplateBinding Button.Content}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="button" Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/MyProject;component/Design/Images/my-image.png" Stretch="None"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="button" Property="Background">
<Setter.Value>
<SolidColorBrush Color="{StaticResource MyCustomColor}"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsFocused" Value="True">
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="button" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</Window.Resources>
Use
<Button x:Name="MyButton"
Style="{StaticResource MainMenuButtonTemplate}"
Margin="0,50,0,0"
Width="auto"
Height="60"
Command="{Binding FindrViewCommand}"
BorderBrush="{x:Null}"
Foreground="White" >
<Button.Background>
<ImageBrush ImageSource="Design/Images/my-hover-image.png" Stretch="None"/>
</Button.Background>
</Button>