I am learning C# and XAML to build windows applications. I wanted to create a button that has an image as its background. But when hovering over the button, the background of the button should change to another "highlighted" image. I attempted to add the background images into Resources.resx. I had to create a custom button using xaml styles to get rid of the default highlight effect of a wpf button.
I created a custom button from some code I found on SO. The code is (in a new resource dictionary):
<!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
<Style x:Key="StartMenuButtons" 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">
<!-- UPDATE THE BUTTON BACKGROUND -->
<Setter Property="Background" Value="WHAT GOES HERE" TargetName="border"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What do I put so that the background changes to another image, whether it is in my resources.resx or another location? (Not sure where to put the image to access it). I searched SO but the solutions I found were not exactly what I am dealing with. If this is a duplicate question, I apologize.
Summary:
How do I change the background image of a button on a mouse over trigger in XAML?
Where do I put the image so that it can be accessed in the trigger code?
Update
This is what I have put as the trigger action, but the image does not update. I made sure to set the image build action to resource and put it in a folder called Resources.
The code is:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/Simon;component/Resources/btn_bg_hover.jpg" />
</Setter.Value>
</Setter>
</Trigger>
The file structure is
Simon
Simon
Resources
all the images
Fonts
bin
obj
Properties
Solution
The following is the complete code to allow for a mouseover image change on the button:
<!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
<Style x:Key="StartMenuButtons" 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/btn_bg_hover.jpg" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
For the actual image, I placed it in the Resources folder that is in the root directory. After importing the images in there using the resources tool in visual studio, I updated the image build settings to Resource in the Properties pane.
Thanks for the solution dbaseman
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…