How to set mouseover / trigger event for border in XAML?

How to set mouseover / trigger event for border in XAML? I want the border to turn green when the mouse is above it, and then to go back to blue when the mouse is no longer above the border. Why can't I use only the following code:

<Border Name="ClearButtonBorder" Grid.Column="1" CornerRadius="0,3,3,0" Background="Blue"> <Border.Triggers> <Trigger Property="Border.IsMouseOver" Value="True"> <Setter Property="Border.Background" Value="Green" /> </Trigger> <Trigger Property="Border.IsMouseOver" Value="False"> <Setter Property="Border.Background" Value="Blue" /> </Trigger> </Border.Triggers> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" /> </Border> 

I really don't get these triggers and styles in WPF. It is such a resistance not to be able to achieve something as simple as this. Please provide me a solution and help me understand what the hell is wrong with my code? Thank you very much.

+59
triggers wpf xaml
Mar 05 '10 at 16:52
source share
1 answer

Yes, this is confusing ...

According to this blog post , it looks like this is an omission from WPF.

To do this, you need to use a style:

  <Border Name="ClearButtonBorder" Grid.Column="1" CornerRadius="0,3,3,0"> <Border.Style> <Style> <Setter Property="Border.Background" Value="Blue"/> <Style.Triggers> <Trigger Property="Border.IsMouseOver" Value="True"> <Setter Property="Border.Background" Value="Green" /> </Trigger> </Style.Triggers> </Style> </Border.Style> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" /> </Border> 

I guess this problem is not so common, as most people tend to take this thing into style, so it can be used for several controls.

+117
Mar 05 '10 at 17:14
source share



All Articles