There is a style for the button:

<Style x:Key="RoundButtonStyle" 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="White" /> <Setter TargetName="buttonCaption" Property="Foreground" Value="Red" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

There is a window where the button is announced:

 ... <Button Style="{DynamicResource RoundButtonStyle}" Background="Blue"Foreground="Black" Content="POPO" /> ... 

The Background, Foreground and Content properties of the button are redefined via TemplateBinding.

Question - How to give the opportunity to similarly override the properties in the triger? (At the moment it is "White" and "Red")

    1 answer 1

     <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> 

    enter image description here

    • I apparently did not explain what I mean by the word "similar." You do NOT need to combine the color settings with the cursor and normal mode, and make the second attribute that you can specify when you declare the button. Without climbing and editing the style itself. - Opossum
    • one
      @Opossum, 1) You can create your UserControl, in which you will already have the property responsible for the colors in the triggers. 2) You can define several different brushes in the style and, for example, depending on the Tag property of the button, use the desired brush. 3) Use existing properties that are not used. For example, in BorderBrush put the color that you want to use for the trigger. - trydex
    • I don’t understand how exactly you need to pass parameters through the binding from the usercontrol. As I understand it is necessary to write ( Example ) Width="{Binding RadiusProperty}" Height="{Binding RadiusProperty}" in Width="{Binding RadiusProperty}" Height="{Binding RadiusProperty}" , and in the cs control file to work with this radius through the propdp construction. Just do not understand how to do it. Google did not solve the problem. - Opossum
    • @Opossum, completed the answer. - trydex
    • 2
      @Opossum, It starts to blink, because the color that is set when you hover the cursor is not specified. To set the default color, change the dependency property registration to DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(SuperButton), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Red))); - trydex