Reputation: 2623
I am working on a C# WPF project (VS 2010) having some buttons in various forms and want to set some common properties to all those buttons (e.g: bold text, change color on hover). However I can set properties from Properties tab and hover behavior one by one using the following code.
private void btnOne_MouseHover(object sender, EventArgs e)
{
(sender as Button).BackColor = Color.Orange;
}
private void btnTwo_MouseLeave(object sender, EventArgs e)
{
(sender as Button).BackColor = Color.LightGray;
}
Is there any way to change all buttons properties from a single place? Any example available?
Upvotes: 0
Views: 804
Reputation: 707
My experience with WPF isn't fantastic but could you instead set the MouseHover and MouseLeave as an XAML style (I've wrote this from scratch not in an IDE as i'm on my phone so it may no be exactly right):
<Style x:Key="MyStyle" TargetType="Button">
<Setter Property="Background" Value="GRAY_COLOUR" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="ORANGE_COLOUR" />
</Trigger>
</Style.Triggers>
</Style>
Replacing the GRAY_COLOUR
and ORANGE_COLOUR
with either the hex values for that colour or define them as static resources. Then assign that style to all applicable buttons:
<Button Style="{StaticResource MyStyle}" />
Upvotes: 1
Reputation: 935
In WPF you can do it in XAML.
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Orange"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
Upvotes: 2