没有事件传递到 WPF 装饰层
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你是否已经尝试过:
如果您希望添加的元素对事件做出反应,我认为该元素必须绑定到装饰器的视觉树。
做到这一点的方法是使用一个 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:
This way the events are correctly routed.
我刚刚遇到了同样的问题。按照 MSDN 的建议为我排序:
即在装饰器本身中,确保 IsHitTestVisible = false
I just had the same issue. Following the advice from MSDN sorted it for me:
i.e In the adorner itself, make sure IsHitTestVisible = false