Wpf Button.MouseLeftButtonDown not working at all

I am trying to find out how MouseLeftButtonDown works, but still not seccuss.

When I click on the button, nothing happens.

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Name="sss"> <Button x:Name="b1" Height="213" MouseLeftButtonDown="sss_MouseDown"/> </StackPanel> </Grid> </Window> 

Code behind:

 private void sss_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("3 ->>>>>" + ((Control)sender).Name); } 
+7
c # wpf
source share
3 answers

From the documentation for this event :

Some control classes may have built-in class handling for mouse button events. The left mouse button event is the most likely event for managing classes in the control. Handling a class often marks the main event of the Mouse class as handled. When an event is marked as processed, other instance handlers that are bound to this element usually do not occur. Any other class or instance handlers that are tied to the elements in the direction of the bubbles to the root in the user interface tree also usually do not occur.

In short: the button probably handles this event to generate its own MouseDown and MouseClick . Since the button marks the event as processed, your own handler is not called. Instead, try using one of the more standard events.

The page also lists several workarounds, but I usually avoid these problems and use more standard solutions.

+5
source share

try the simple PreviewMouseLeftButtonDown event

+10
source share

I had the same problem, but the PreviewMouseLeftButtonDown event was used instead. It worked.

+2
source share

All Articles