Mousedown Double Shooting Event (WPF)

I am currently trying to capture multi-downs from an image on a simple grid. I have no problem launching the event, it just shoots twice. And since clicking on it twice in the end will have a different state (it will show an expanded image), switching to the second press causes problems.

My current code is as follows:

Xaml

<Window x:Class="WpfApplication.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 Background="Transparent" x:Name="MainContent" MouseDown="Generic_MouseDown"> <Image Source="http://www.blogcdn.com/www.engadget.com/media/2011/05/welcome-kansas-city-google-high-speed-internet.jpg" Height="100" Width="100" MouseDown="Generic_MouseDown"/> </Grid> </Window> 

the code:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Generic_MouseDown(object sender, MouseButtonEventArgs e) { if (((FrameworkElement)e.Source).GetType() == typeof(System.Windows.Controls.Image)) { Console.Out.WriteLine("image clicked"); } else { Console.Out.WriteLine("grid clicked"); } } } 

So, when I click on the image, it starts mousedown twice.

Thanks!

+6
source share
3 answers
 private void Generic_MouseDown(object sender, MouseButtonEventArgs e) { if (((FrameworkElement)e.Source).GetType() == typeof(System.Windows.Controls.Image)) { Debug.WriteLine("image clicked"); e.Handled = true; } else { Debug.WriteLine("grid clicked"); } } 

You need to set the Handled property to true.

+13
source

You will need to set e.Handled to true in order to prevent the occurrence of an event from image to grid.

In fact, what happens is that the event occurs on the image, then if it is not processed, it occurs on the grid, etc. up the visual tree.

+6
source

This is your XAML and you added [MouseDown = "Generic_MouseDown"] Twice in the grid and image

 <Window x:Class="WpfApplication.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 Background="Transparent" x:Name="MainContent" MouseDown="Generic_MouseDown"> <Image Source="http://www.blogcdn.com/www.engadget.com/media/2011/05/welcome-kansas-city-google-high-speed-internet.jpg" Height="100" Width="100" MouseDown="Generic_MouseDown"/> </Grid> </Window> 

Make ONE [MouseDown = "Generic_MouseDown"] in the Grid

 <Window x:Class="WpfApplication.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 Background="Transparent" x:Name="MainContent" MouseDown="Generic_MouseDown"> <Image Source="http://www.blogcdn.com/www.engadget.com/media/2011/05/welcome-kansas-city-google-high-speed-internet.jpg" Height="100" Width="100" /> </Grid> </Window> 

OR

Make ONE [MouseDown = "Generic_MouseDown"] in Image

 <Window x:Class="WpfApplication.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 Background="Transparent" x:Name="MainContent"> <Image Source="http://www.blogcdn.com/www.engadget.com/media/2011/05/welcome-kansas-city-google-high-speed-internet.jpg" Height="100" Width="100" MouseDown="Generic_MouseDown"/> </Grid> </Window> 
+1
source

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


All Articles