Mousedown事件触发两次(WPF)

我目前正在尝试从简单网格上的图像捕获mousedown。我没有触发事件,只是它触发了两次。而且,由于两次单击最终将具有不同的状态(它将显示展开的图像),因此直接单击第二次会引起问题。 我当前的代码如下: 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\");
        }

    }
}
因此,当我单击图像时,它会两次按下鼠标。 谢谢!     
已邀请:
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。     
您需要将e.Handled设置为true,以防止事件从Image冒泡到Grid。 实际上,正在发生的事件是在Image上引发事件,然后,如果未处理,则在Grid上引发事件,依此类推。     
这是您的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>
使其像网格中的一个[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>
要么 使其像图像中的一个[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>
    

要回复问题请先登录注册