Wpf event does not bubble

Here is my XAML:

<Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="844.025" Width="678" MouseUp="somethingClicked"> <Grid MouseUp="somethingClicked"> <StackPanel MouseUp="somethingClicked" Margin="0,0,10,0"> <Button x:Name="btnClickMe" Content="Click Me!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="101,22,0,0" MouseUp="somethingClicked"/> <CheckBox x:Name="chkhandle" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="241,28,0,0" RenderTransformOrigin="-0.588,1.188"/> <ListBox x:Name="lstEvents" HorizontalAlignment="Left" Height="604" VerticalAlignment="Top" Width="416" Margin="29,66,0,0"/> </StackPanel> </Grid> 

And here is the C # code:

 namespace WpfApplication4 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { protected int eventCounter = 0; public MainWindow() { InitializeComponent(); } private void somethingClicked(object sender, RoutedEventArgs e) { eventCounter++; String message = "#" + eventCounter.ToString() + ":\r\n" + " Sender: " + sender.ToString() + ":\r\n" + " Source: " + e.Source + ":\r\n" + " Original Source: " + e.OriginalSource; lstEvents.Items.Add(message); e.Handled = (bool) chkhandle.IsChecked; if (e.Handled) lstEvents.Items.Add("Completed"); } } 

}

I have the following problems with this example: 1) The MouseUp event does not fire when a button is clicked. 2) The event does not bubble. By clicking on any form, you will see:

 Sender:WpfApplication4.MainWindow: Source:WpfApplication4.MainWindow: Original Source: System.Windows.Controls.Border. 

If I understand correctly when the button is clicked, it must first run at the window level (what it is doing now), then Grid, then it folds and finally the text label. Is the code wrong or is my understanding of the concept wrong?

+4
source share
4 answers

The MouseUp event is not fired on clicking the button.

Since the first fires are an event in Button.Click , and when it works, it conflicts with the MouseUp event. Quote from here :

ButtonBase inherits from UIElement, the button will also have access to all mouse events defined for UIElement. Because Button does something in response to button presses, it swallows bubbling events (like MouseLeftButtonDown and MouseDown). You can still detect these low-level button click events by adding handlers for tunneling events (e.g. PreviewMouseLeftButtonDown and PreviewMouseDown).

Try replacing Button with Label , and you will get the desired result:

enter image description here

+4
source

Microsoft wrote a very nice explanation Review of routed events

enter image description here

the same thing will happen with the MouseUp and PreviewMouseUp events in your case e.Handled = (bool) chkhandle.IsChecked; stops event routing.

if you want to debug events that you can use Snoop , it will very well illustrate what events happened, on which objects and who processed them.

+1
source
  • Instead, try to handle the PreviewMouseDown event. You can still attach this from XAML. In your handler

  • Attach an event handler instead of code. Use the AddHandler signature

.

 private void Window_Loaded(object sender, RoutedEventArgs e) { Grid1.MouseUp += new MouseButtonEventHandler(Grid1_MouseUp); } private void Grid1_MouseUp(object sender, MouseButtonEventArgs e) { MessageBox.Show("Mouseup"); } 
+1
source

There is an override available to handle events, even if they were marked as processed. To do this, you need to add a handler through the code as follows:

 MainWindow.AddHander(UIElement.MouseUpEvent, new MouseButtonEventHandler(button1_MouseUp), true); 

This last parameter indicates whether you want to accept events that have already been processed or not. If you add a handler to the main window, you will notice that the MouseUp routed events from your button do indeed bubble (but their e.Handled indicates that they have already been processed).

0
source

Source: https://habr.com/ru/post/1497921/


All Articles