Wpf: left click not recognized

I am recently in WPF and when I study the material, I came across a strange problem.

I create a button that contains layers with a text block, and I want to find out where the user clicks on the button itself, on the "first", "second" or "third" (I display a message).

enter image description here

Everything works fine, except that the button does not trigger an event when the user clicks the left button (just the middle or right button).

so my question is: why didn’t I get a message box when I click on the button itself with the left mouse button (and I get a message box with other mouse buttons)?

XAML:

<Button Margin="145,152,144,102" Padding="5,5,5,5" HorizontalAlignment="Center" VerticalAlignment="Center" MouseDown="Button_MouseDown" Height="57" Width="214"> <WrapPanel> <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center"></WrapPanel> <TextBlock Foreground="Black" FontSize="24" MouseDown="TextBlockFirst_MouseDown" >First </TextBlock> <TextBlock Foreground="Red" FontSize="24" MouseDown="TextBlockSecond_MouseDown">Second </TextBlock> <TextBlock Foreground="Blue" FontSize="24" MouseDown="TextBlockThird_MouseDown" >Third </TextBlock> </WrapPanel> </Button> 

The code:

 private void TextBlockFirst_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on first"); } private void TextBlockSecond_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on second"); } private void TextBlockThird_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on third"); } private void Button_MouseDown(object sender, MouseButtonEventArgs e) { // This event not working good // only middle & right mouse buttons are recognized MessageBox.Show("You click on the button"); } 

Thanks!

+9
c # events wpf mouseevent
source share
2 answers
Event

MouseDown is a bubbling event that bubbles from its creator to its parent root. But the Click event eats up the MouseDown event and does not allow the event to MouseDown over the buttons.

You can use the PreviewMouseDown event, which is a tunnelling event that tunnels from the root to its creator. Thus, the button will receive this event first, and then the subsequent text box.

 <Button PreviewMouseDown="Button_MouseDown"> ....... </Button> 

Pay attention to the picture below for a clear image:

enter image description here


UPDATE

Connect only the PreviewMouseDown button to the event and remove handlers from individual text blocks. Check e.OrignialSource to see if the TextBlock actual source or button.

 private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (!(e.OriginalSource is TextBlock)) { MessageBox.Show("You click on the button"); } else { switch ((e.OriginalSource as TextBlock).Text) { case "First": MessageBox.Show("You click on first"); break; case "Second": MessageBox.Show("You click on second"); break; case "Third": MessageBox.Show("You click on third"); break; } } } 

Xaml

 <Button PreviewMouseDown="Button_PreviewMouseDown" Height="57" Width="214"> <WrapPanel> <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBlock Foreground="Black" FontSize="24">First</TextBlock> <TextBlock Foreground="Red" FontSize="24">Second</TextBlock> <TextBlock Foreground="Blue" FontSize="24">Third</TextBlock> </WrapPanel> </Button> 
+15
source share

This does not work, because the first fires are an event in Button.Click , and when it works, it conflicts with events such as: MouseLeftButtonDown , MouseUp , MouseDown .

To handle this event, you need to define the PreviewMouseDown event, because it is a Tunnel event, it means that it will drop from the VisualTree hierarchy, so it fires before Bubble events.

Alternatively, you can use the Button.Click event for the button.

+2
source share

All Articles