Mousedown 事件触发两次 (WPF)

发布于 2024-11-07 19:48:17 字数 1251 浏览 0 评论 0原文

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

我当前的代码如下:

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 两次。

谢谢!

I am currently trying to capture a mousedown from an image on a simple grid. I have no problems with the event firing, it is just that it fires twice. And because clicking it twice will eventually have a different state (it will show an expanded image), going straight to second click is causing 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>

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 the image, it fires mousedown twice.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰之心 2024-11-14 19:48:17
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。

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.

悲欢浪云 2024-11-14 19:48:17

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

实际上,发生的情况是在图像上引发事件,然后如果未处理该事件,则会在网格上引发事件,依此类推,在可视化树上引发。

You would need to set e.Handled to true to prevent the event from bubbling up from the Image to the Grid.

Effectively, what is happening is the event is raised on the Image, then if it's not handled it is raised on the Grid, and so on up the visual tree.

み格子的夏天 2024-11-14 19:48:17

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

This is your XAML & you added [ MouseDown="Generic_MouseDown" ] Twice to 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>

Make It Like that ONE [ MouseDown="Generic_MouseDown" ] in 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 It Like that 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文