I am trying to make an image button that changes the image when the mouse is over the button, i tried few things
this is the last thing I tried but it doesn't work:
<Button Name="fileNameLink" Margin="15,6,30,1" VerticalAlignment="Top" Click="btnMinimize_Click" MaxWidth="250" Background="Transparent" Cursor="Hand" Visibility="Visible" Height="16">
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel>
<Image Name="image1" Source="{StaticResource Minimize1}" Stretch="None" Visibility="Collapsed"/>
<Image Name="image2" Source="{StaticResource Minimize2}" Stretch="None" Visibility="Visible"/>
<StackPanel.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="image1" Property="Visibility" Value="Visible"/>
<Setter TargetName="image2" Property="Visibility" Value="Collapsed"/>
</Trigger>
</StackPanel.Triggers>
</StackPanel>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
i am tring to use the XAML only, without the cs file Thanks
Here is another approach to handle it using a specialized style. It should be pointed out, that this could be further improved with using attached properties for customizing the image files properly.
The Rectangle is important for the hit testing, otherwise the IsMouseOver won't trigger. And the Stretch on the ContentAlignment is necessary otherwise the ContentPresenter of the button would not span the whole button, therefore IsMouseOver wouldn't trigger again.
You will need a helper class to attach the image source property per button state and a style for your button. Put the helper class in a
Helpers
folder in your WPF project.Helper class
Button Style
Usage
Your Trigger's are applied to the
StackPanel
. It needs to be set on theControlTemplate
Try:
In such cases. You can just use the
Trigger
to set theImage
's source thereby not having to play with switchingVisibility
on multiple UI element's.something like:
You can achieve this by
Image
style: