Good day, Lord. There was a need to make SearchBox. The essence of the question is as follows: TextBox + Button I put in the UserControl. When you hover the mouse over the UserControl, you need to change the Background / Foreground of this UserControl and accordingly make my TextBox and button change to these values. I implemented this task. Here is the code:

<UserControl x:Class="PTRCPriceCalculator.SearchBox" 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:PTRCPriceCalculator" mc:Ignorable="d" Style="{StaticResource UserControlStyle}" d:DesignHeight="40" d:DesignWidth="300"> <UserControl.Template> <ControlTemplate TargetType="{x:Type UserControl}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="40"/> </Grid.ColumnDefinitions> <TextBox Grid.Column="0" Style="{StaticResource DarkTextBox}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Text="Search..."/> <Button Grid.Column="1" Style="{StaticResource InnerSearchButton}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}"/> </Grid> </ControlTemplate> </UserControl.Template> </UserControl> 

Here is the UserControl style:

 <Style TargetType="{x:Type UserControl}" x:Key="UserControlStyle"> <Setter Property="Background" Value="{StaticResource BackgroundMediumDarkBrush}"/> <Setter Property="Foreground" Value="{StaticResource ForegroundLightBrush}"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="{StaticResource BackgroundDarkBrush}"/> <Setter Property="Foreground" Value="{StaticResource ForegroundVeryLightBrush}"/> </Trigger> </Style.Triggers> </Style> 

And the question is that it is necessary for me that if the user directs the mouse specifically on the button, then she has to change the properties of the Background / Foreground differently, uniquely. But the fact is that my UserControl trigger overrides the button trigger, and accordingly when I click the button with the mouse, nothing unique happens.

Do not tell me how to solve the problem?

Here is the style of the InnerSearchButton:

 <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Content}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="{StaticResource BackgroundVeryDarkBrush}"/> <Setter Property="Foreground" Value="Red"/> </Trigger> </Style.Triggers> 

  • “My UserControl trigger overrides the button trigger” - where is the button trigger in your code? Show the best style InnerSearchButton . - VladD
  • And how to throw off the code in the comments? It is multiline. Yes, there is a pretty simple style - Bretbas
  • @Bretbas can be used for example gist.github - user227049
  • @Bretbas: The code should be in question. Edit the question, there is a link "edit". - VladD
  • Everything, I corrected my question - Bretbas

1 answer 1

The problem is in the order of applying values ​​of dependency property . The locally set value (No. 3 in the list) is always stronger than the value defined in the style (No. 8 in the list) and the style triggers (No. 6 in the list).

So Background="{TemplateBinding Background}" overlaps the values ​​from the style. Therefore, you do not need to set the Background explicitly, but create a derived style in which to add the necessary setters.

  • I would like to do this: I have a SearchBox with an internal button. It is necessary to do so that I can apply different styles for SearchBox as a whole, but inside this style, it was possible to define the style for the button so that it was also internal - Bretbas
  • @Bretbas: I will try to write later when there is time. - VladD
  • @Bretbas: Look here: ru.stackoverflow.com/a/752780/10105 - VladD