Reputation: 3252
I have the following XAML:
<UserControl x:Class="WPFSandpit.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Border BorderThickness="1" BorderBrush="Blue" Width="100" Height="35" PreviewMouseLeftButtonUp="OnPreviewMouseLeftButtonUp">
<Grid >
<Grid.Style>
<Style>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
<TextBlock Text="MyText" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
FontWeight="SemiBold"/>
</Grid>
</Border>
The code for the event handler simply shows a message box:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Selected");
}
}
I'm having difficulty understanding why the event is only being fired when in the TextBlock and not when using the mouse within the Border itself?
Upvotes: 1
Views: 1066
Reputation: 122
I assume, that you want the textbox to turn red when you are over the border, instead when you are inside the border (If you really mean triggering the OnPreviewMouseLeftButtonUp, you must put your question more clearly, as for me it seems to trigger just fine with your code). Based on that assumption you simply placed the style in the wrong scope. When applying the style to the border node in the xaml it behaves as expected as shown in the code snippet below. Note that I increased the border width to easier trigger the event as well as the color change.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Border BorderThickness="10" BorderBrush="Blue" Width="100" Height="35" PreviewMouseLeftButtonUp="OnPreviewMouseLeftButtonUp">
<Border.Style>
<Style>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid >
<TextBlock Text="MyText" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
FontWeight="SemiBold"/>
</Grid>
</Border>
</Window>
And the c# code:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Selected" + sender.ToString());
}
}
}
Upvotes: 1
Reputation: 61
At first glance it looks like the text box is handling the event and simply not bubbling up the event since TextBox will handle all mouse events such as preview, capture, drag, etc. If you get the event without the TextBox in XAML then this is probably what is happening.
Not sure what the end goal is based on your code but whatever you planned to do for that event in the border event handler would need to get handled in the TextBox event handler. Alternatively, you may be able to handle the event in TextBox (create event handler), call the base handler, then set Handled to false.
Upvotes: 0