没有事件传递到 WPF 装饰层

发布于 2024-09-03 18:46:30 字数 882 浏览 2 评论 0原文

我正在尝试在 WPF 中创建一个漂亮的“拖放区域”,当将某些内容拖到主应用程序中时,该区域会显示在装饰器层中。问题是我没有从我的装饰器那里得到任何事件,尽管根据文档它应该接收所有输入事件,因为它处于更高的 z 顺序。

为了调试我的问题,我创建了一个非常简单的示例,其中我有一个只有一个按钮的用户控件。该用户控件显示在装饰层中,但我无法单击该按钮。为什么?我做错了什么?

我的装饰器类的构造如下:

    public ShellOverlayAdorner(UIElement element, AdornerLayer adornerLayer)
        :base(element)
    {
        _adornerLayer = adornerLayer;

        _overlayView = new AdornedElement();
        _overlayView.AllowDrop = true;
        _adornerLayer.Add(this);
     }

并在主窗口中创建,

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        adornerLayer = AdornerLayer.GetAdornerLayer(MyTopGridWithButtonInIt);
        ShellOverlayAdorner shell = new ShellOverlayAdorner(MyTopGridWithButtonInIt, adornerLayer);

    }

我根本没有从我的控件中获取任何事件,即没有鼠标单击、鼠标悬停、按钮单击。我什至无法单击装饰层中的按钮。我做错了什么?

I am trying to make a nice "drag and drop zone" in WPF that is displayed in the adorner layer when something is being dragged into the main application. The problem is that I do not get any events from my adorner, even though it according to documentation should receive all input events since it is in a higher z-order.

To debug my problem I created a really simple example where I have a user control with only a button in it. This user control is displayed in the adorner layer, but I cannot click the button. Why? What have I done wrong?

My adorner class is constructed like this:

    public ShellOverlayAdorner(UIElement element, AdornerLayer adornerLayer)
        :base(element)
    {
        _adornerLayer = adornerLayer;

        _overlayView = new AdornedElement();
        _overlayView.AllowDrop = true;
        _adornerLayer.Add(this);
     }

and is created in the main window by

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        adornerLayer = AdornerLayer.GetAdornerLayer(MyTopGridWithButtonInIt);
        ShellOverlayAdorner shell = new ShellOverlayAdorner(MyTopGridWithButtonInIt, adornerLayer);

    }

I do not get any events at all from my control, i.e. no mouse clicks, mouse over, button clicks. I cannot even click the button in the adorner layer. What have I done wrong?

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

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

发布评论

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

评论(2

≈。彩虹 2024-09-10 18:46:30

我不知道你是否已经尝试过:
如果您希望添加的元素对事件做出反应,我认为该元素必须绑定到装饰器的视觉树。
做到这一点的方法是使用一个 VisualCollection,对装饰器本身进行初始化,或者至少,这样它似乎可以工作:

    VisualCollection visualChildren;
    FrameworkElement @object;

    public CustomAdorner(UIElement adornedElement) :
        base(adornedElement)
    {
        visualChildren = new VisualCollection(this);
        @object = new Button {Content = "prova"};
        visualChildren.Add(@object);
    }
    protected override Visual GetVisualChild(int index)
    {
        return visualChildren[index];
    }

这样事件就可以正确路由。

I don't know if you already tried that:
If you want the element added to react to events, I think that the element must be bound to the visual tree of the adorner.
The way to do it is to use a VisualCollection, intitialized to the adorner itself, or at least, this way it seems to be working:

    VisualCollection visualChildren;
    FrameworkElement @object;

    public CustomAdorner(UIElement adornedElement) :
        base(adornedElement)
    {
        visualChildren = new VisualCollection(this);
        @object = new Button {Content = "prova"};
        visualChildren.Add(@object);
    }
    protected override Visual GetVisualChild(int index)
    {
        return visualChildren[index];
    }

This way the events are correctly routed.

烟花肆意 2024-09-10 18:46:30

我刚刚遇到了同样的问题。按照 MSDN 的建议为我排序:

装饰器仅接收输入事件
与任何其他 FrameworkElement 一样。
因为装饰者总是有更高的
z 顺序比它装饰的元素,
装饰器接收输入事件
(例如 Drop 或 MouseMove)
用于底层装饰
元素。装饰者可以聆听
某些输入事件并将其传递
到底层的装饰元素
重新引发该事件。

启用直通命中测试
装饰器下的元素,设置命中
测试 IsHitTestVisible 属性
装饰器上为 false。

即在装饰器本身中,确保 IsHitTestVisible = false

I just had the same issue. Following the advice from MSDN sorted it for me:

Adorners receive input events just
like any other FrameworkElement.
Because an adorner always has a higher
z-order than the element it adorns,
the adorner receives input events
(such as Drop or MouseMove) that may
be intended for the underlying adorned
element. An adorner can listen for
certain input events and pass these on
to the underlying adorned element by
re-raising the event.

To enable pass-through hit testing of
elements under an adorner, set the hit
test IsHitTestVisible property to
false on the adorner.

i.e In the adorner itself, make sure IsHitTestVisible = false

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