LeftMouseButtonDown 不适用于路由事件中的按钮

发布于 2024-07-23 12:25:13 字数 1530 浏览 6 评论 0原文

我正在学习 wpf 中的路由事件,我尝试了以下示例:

文件 -- Window1.xaml

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <UniformGrid MouseDown="UniformGrid_MouseDown">
        <Button x:Name="Button1">1</Button>
        <Button x:Name="Button2">2</Button>
        <Button x:Name="Button3">3</Button>
        <Button x:Name="Button4">4</Button>
        <Button x:Name="Button5">5</Button>
        <Button x:Name="Button6">6</Button>
        <Button x:Name="Button7">7</Button>
        <Button x:Name="Button8">8</Button>
        <Button x:Name="Button9">9</Button>
    </UniformGrid>
</ScrollViewer>

文件 -- Window1.xaml.cs

private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
    Button aTargetButton = e.Source as Button;
    if (aTargetButton != null)
    {
        aTargetButton.Background = Brushes.Azure;
        aTargetButton.LayoutTransform = new RotateTransform(45);
        if (myPreviouslyClickedButton != null)
        {
            myPreviouslyClickedButton.Background = Brushes.White;
            myPreviouslyClickedButton.LayoutTransform = new RotateTransform(0);
        }
        myPreviouslyClickedButton = aTargetButton;
    }
}

当我运行这些片段时,仅当我右键单击时,相应的按钮才会经历角度转换它(即使我已经订阅了 MouseDown)。 你能帮我解决这个问题吗?

更新:

如果我用椭圆形替换按钮,这个片段似乎可以工作。 为什么按钮不能对左键单击做出反应,而椭圆可以。 此外,如果我多次单击同一个椭圆,则不会引发事件

I was learning about routed events in wpf and I tries the following example,

File -- Window1.xaml

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <UniformGrid MouseDown="UniformGrid_MouseDown">
        <Button x:Name="Button1">1</Button>
        <Button x:Name="Button2">2</Button>
        <Button x:Name="Button3">3</Button>
        <Button x:Name="Button4">4</Button>
        <Button x:Name="Button5">5</Button>
        <Button x:Name="Button6">6</Button>
        <Button x:Name="Button7">7</Button>
        <Button x:Name="Button8">8</Button>
        <Button x:Name="Button9">9</Button>
    </UniformGrid>
</ScrollViewer>

File -- Window1.xaml.cs

private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
    Button aTargetButton = e.Source as Button;
    if (aTargetButton != null)
    {
        aTargetButton.Background = Brushes.Azure;
        aTargetButton.LayoutTransform = new RotateTransform(45);
        if (myPreviouslyClickedButton != null)
        {
            myPreviouslyClickedButton.Background = Brushes.White;
            myPreviouslyClickedButton.LayoutTransform = new RotateTransform(0);
        }
        myPreviouslyClickedButton = aTargetButton;
    }
}

When I ran these snippets, the corresponding button undergoes a Angular Transformation only when I do a right click over it (even though I have subscribed for MouseDown). Can you help me out with this?

Update:

This snippet seems to work if i replace the button with an ellipse. Why can't the button react to left clicks when an ellipse can. Also the events are not raised if i click on the same ellipse more then once

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

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

发布评论

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

评论(2

这个俗人 2024-07-30 12:25:13

您的鼠标按下事件由按钮处理。

如果您希望网格处理按钮事件,那么您可以执行以下操作:

 <ScrollViewer VerticalScrollBarVisibility="Auto">
    <UniformGrid Button.Click="UniformGrid_Click">
        <Button x:Name="Button1" Margin="10">1</Button>
        <Button x:Name="Button2">2</Button>
        <Button x:Name="Button3">3</Button>
        <Button x:Name="Button4">4</Button>
        <Button x:Name="Button5">5</Button>
        <Button x:Name="Button6">6</Button>
        <Button x:Name="Button7">7</Button>
        <Button x:Name="Button8">8</Button>
        <Button x:Name="Button9">9</Button>
    </UniformGrid>
    </ScrollViewer>

并将源更改为类似以下内容:

private void UniformGrid_Click(object sender, RoutedEventArgs e)
    {
        // Your code here
    }

有一篇 msdn 文章 这里值得一读。

Your mouse down event is being handled by the button.

If you want your grid to handle the button events, then you might do something like:

 <ScrollViewer VerticalScrollBarVisibility="Auto">
    <UniformGrid Button.Click="UniformGrid_Click">
        <Button x:Name="Button1" Margin="10">1</Button>
        <Button x:Name="Button2">2</Button>
        <Button x:Name="Button3">3</Button>
        <Button x:Name="Button4">4</Button>
        <Button x:Name="Button5">5</Button>
        <Button x:Name="Button6">6</Button>
        <Button x:Name="Button7">7</Button>
        <Button x:Name="Button8">8</Button>
        <Button x:Name="Button9">9</Button>
    </UniformGrid>
    </ScrollViewer>

and change the source to something like:

private void UniformGrid_Click(object sender, RoutedEventArgs e)
    {
        // Your code here
    }

There's an msdn article here that would be worth a read.

辞别 2024-07-30 12:25:13

这是正常工作的。 在内部,Button 处理 MouseDown 事件以了解何时触发其 Click 事件。 我发现 Snoop 是一个很好的工具,用于跟踪 WPF 事件问题,或者只是获取更好地了解它们的工作原理。

This is working correctly. Internally, the Button handles the MouseDown event to know when to fire off its Click event. I've found that Snoop is a great tool for tracking down problems with WPF events, or to just get a better understanding of how they work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文