6

我目前正在尝试从简单网格上的图像中捕获鼠标按下。我对事件触发没有任何问题,只是它触发了两次。并且因为单击它两次最终将具有不同的状态(它将显示展开的图像),所以直接单击第二次会导致问题。

我目前的代码如下:

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

    }
}

因此,当我单击图像时,它会触发 mousedown 两次。

谢谢!

4

3 回答 3

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

于 2011-05-17T17:57:33.437 回答
6

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

实际上,正在发生的事情是在 Image 上引发事件,然后如果未处理,则会在 Grid 上引发事件,依此类推,直到可视化树。

于 2011-05-17T17:55:38.143 回答
2

这是您的 XAML 并且您将 [ MouseDown="Generic_MouseDown" ]两次添加到 Grid & 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>

让它像网格中的一个[ 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>

或者

让它像图像中的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>
于 2011-05-17T23:11:28.660 回答