<Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="buttonSurface" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> <Setter TargetName="buttonCaption" Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}" /> </Trigger>
We create UserControl and inherit from Button to save all the functionality of the standard button. Create a dependency property in which the color will be stored:
public partial class SuperButton : Button { public SuperButton() { InitializeComponent(); } public Brush MouseOverBrush { get { return (Brush)GetValue(MouseOverBrushProperty); } set { SetValue(MouseOverBrushProperty, value); } } public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(SuperButton)); }
SuperButton.xaml will look like this:
<Button x:Class="Example.SuperButton" x:Name="button" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Example" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid> <Ellipse x:Name="buttonSurface" Fill="{TemplateBinding Control.Background}"/> <Label x:Name="buttonCaption" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Content="{TemplateBinding Button.Content}" Foreground="{TemplateBinding Control.Foreground}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="buttonSurface" Property="Fill" Value="{Binding ElementName=button, Path=MouseOverBrush}" /> <Setter TargetName="buttonCaption" Property="Foreground" Value="Red" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style>
Now in the trigger, we tie the color to the dependency property. Everything.
MainWindow.xaml:
<Grid> <local:SuperButton MouseOverBrush="BlueViolet" Width="100" Height="50" /> </Grid>
