没有事件传递到WPF装饰器层 [英] No events passed to WPF adorner layer

查看:105
本文介绍了没有事件传递到WPF装饰器层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF中创建一个很好的拖放区域,当将某些内容拖到主应用程序中时,该区域将显示在装饰层中。问题是,即使根据文档它应该接收所有输入事件,我的装饰者也没有收到任何事件,因为它的Z顺序更高。

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

并由

    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?

推荐答案

我不知道您是否已经尝试过:
如果您想要添加元素以对事件做出反应,我认为该元素必须绑定到装饰物的视觉树上。
做到这一点的方法是使用 VisualCollection ,它与装饰器本身息息相关,或者至少看起来是这样的:

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.

这篇关于没有事件传递到WPF装饰器层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆