2011-05-17 61 views
6

我目前正尝试从简单网格上的图像中捕获mousedown。我在射击事件方面没有问题,只是它发射了两次。而且因为点击两次最终会有不同的状态(它会显示一个扩展的图像),直接点击第二次就会导致问题。Mousedown事件发射两次(WPF)

我当前的代码如下:

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> 

代码:

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"); 
     } 

    } 
} 

所以,当我点击的形象,它触发鼠标按下两次。

谢谢!

回答

6

您需要将e.Handled设置为true以防止事件从图像冒泡到网格。

实际上,发生的事件是图像上发生的事件,如果未处理它,则会在网格上引发,等等。

13
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"); 
    } 

} 

您需要将Handled属性设置为true。

+1

哇,这是比我想象的更容易!非常感谢! :) – Angelus 2011-05-17 18:03:54

+0

经过一个多小时的搜索和调试,我终于找到了这个! – Evils 2013-08-16 13:29:05

1

这是你的XAML &你加入[的MouseDown = “Generic_MouseDown”] 两次网格&图片

<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> 

使它象是ONE [的MouseDown = “Generic_MouseDown” 在电网

<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

使它象是ONE [的MouseDown = “Generic_MouseDown” 在图片

<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>