Is it possible to trigger one object through triggers after the occurrence of an event in another? For example, change the color of one rectangle if the cursor is over another rectangle.

To ourselves, we change the fill so much, and how to act not on ourselves?

<Window.Resources> <Style x:key="changeFill" TargetType="{x:Type Rectangle}" <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Fill" Value="Blue"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <Rectangle x:Name="rect1" Width="20" Height="20" Style="{StaticResource changeFill}"> <Rectangle x:Name="rect2" Width="20" Height="20"> </StackPanel> 
  • 2
    If the style is inside a control, you can use TargetName, it seems. - VladD
  • @VladD it should be the answer, not a comment, it seems;) - user227049
  • @FoggyFinder: Well, for example from the question it does not fit, so probably still not the answer. - VladD

1 answer 1

One solution is to use DataTrigger :

  <Style x:Key="changeFill" TargetType="{x:Type Rectangle}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=rect2, Path=IsMouseOver}" Value="True"> <Setter Property="Rectangle.Fill" Value="Green"></Setter> </DataTrigger> <DataTrigger Binding="{Binding ElementName=rect2, Path=IsMouseOver}" Value="False"> <Setter Property="Rectangle.Fill" Value="Red"></Setter> </DataTrigger> </Style.Triggers> </Style> 

As VladD wrote in the comments , you can use the TargetName property if the name is within the scope of the style.