2011-05-17 64 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>