WPF ScrollViewer/Canvas 鼠标事件处理程序

发布于 2024-12-21 11:41:14 字数 1066 浏览 3 评论 0原文

我创建了以下控件:

<UserControl x:Class="FooBar.AnnotationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500" >
    <ScrollViewer Height="400" Width="500">
        <Canvas Height="400" Width="500" Name="ctlCanvas" MouseLeftButtonDown="MouseLeftButtonDownHandler" >
            <Canvas.RenderTransform>
                <ScaleTransform x:Name="ZoomTransform" />
            </Canvas.RenderTransform>
        </Canvas>
    </ScrollViewer>
</UserControl>

namespace FooBar
{

    public partial class AnnotationControl : UserControl
    {
        public AnnotationControl()
        {
            InitializeComponent();

        }

        private void MouseLeftButtonDownHandler( object sender, MouseButtonEventArgs args)
        {
           //Do Something
        }

    }

}

当我单击画布时,我不会在 MouseLeftButtonDownHandler 中命中断点。我什至将此处理程序附加到 ScrollViewer 并得到相同的结果。知道这是怎么回事吗?

I've created the following control:

<UserControl x:Class="FooBar.AnnotationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500" >
    <ScrollViewer Height="400" Width="500">
        <Canvas Height="400" Width="500" Name="ctlCanvas" MouseLeftButtonDown="MouseLeftButtonDownHandler" >
            <Canvas.RenderTransform>
                <ScaleTransform x:Name="ZoomTransform" />
            </Canvas.RenderTransform>
        </Canvas>
    </ScrollViewer>
</UserControl>

namespace FooBar
{

    public partial class AnnotationControl : UserControl
    {
        public AnnotationControl()
        {
            InitializeComponent();

        }

        private void MouseLeftButtonDownHandler( object sender, MouseButtonEventArgs args)
        {
           //Do Something
        }

    }

}

when I click the canvas, I don't hit breakpoints in the MouseLeftButtonDownHandler. I even attach this handler to the ScrollViewer and get the same result. Any idea what's going on here?

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

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

发布评论

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

评论(1

下壹個目標 2024-12-28 11:41:15

Canvas 的默认背景是透明的,这允许命中测试通过它。要使您的 Canvas 注册 HitTests,请为其指定背景颜色。

<Canvas Background="White" ... />

The default background for a Canvas is Transparent, which allows hit tests to pass through it. To make your Canvas register for HitTests, give it a Background Color.

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