WPF 中的弹出窗口立即失去焦点并关闭
我一直在努力解决 WPF 弹出问题,希望有人能帮助我:) 大纲如下:
我们的应用程序存储 UIObject 列表 - 这些代表显示在视口 3d 中的元素(通过成员 UIRenderObject,它只是一个带有位图的平面方形网格)和树视图中显示的元素(通过成员 TreeViewItem)。
在任何一种情况下,当对象检测到右键单击时,它都会尝试打开弹出窗口。 其代码非常简单:
ContextMenu contextMenu = new ContextMenu();
//add content
contextMenu.IsOpen = true;
当我在视口中单击时,一切都按预期工作 - 弹出窗口打开,并保持打开状态,直到失去焦点或做出选择。 然而,在树视图中,弹出窗口会失去焦点并立即关闭。
我尝试过显式设置焦点,显式设置 PlacementTarget,将鼠标事件标记为在调用打开弹出窗口之前和之后处理,将弹出窗口完全标记为“StaysOpen”以及其他一些事情,但它仍然立即关闭。
任何帮助将不胜感激。提前致谢!
I've been beating my head against a WPF Popup issue, and am hoping someone can help me:)
The outline goes like this:
Our app stores a list of UIObjects - these represent elements that are displayed in a viewport 3d (via a member UIRenderObject, which is just a flat, square mesh with a bitmap on it) and in a treeview (via a member TreeViewItem).
In either case, when the object detects a right click, it tries to open a popup.
The code for that is about as straight forward as you can get:
ContextMenu contextMenu = new ContextMenu();
//add content
contextMenu.IsOpen = true;
When I click in the viewport, everything works as expected - the popup opens, and stays open until it loses focus or a selection is made.
In the tree-view, however, the popup loses focus and closes immediately.
I've tried explicitly setting focus, explicitly setting the PlacementTarget, marking the mouse event as handled before and after the call to open the popup, flat-out marking the popup as "StaysOpen" and a dozen other things, and still it closes immediately.
Any help will be much appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唉,事实证明我把事情复杂化了。
与其自己管理上下文菜单,不如简单地将其分配给 TreeViewItem 自己的 ContextMenu 并让系统处理它,这是一种可行的方法。
那我就学学吧
感谢大家的意见:)
Sigh, as it turns out I was over-complicating the matter.
Rather than managing the context menu myself, simply assigning it to TreeViewItem's own ContextMenu and letting the system deal with it, was the way to go.
That'll learn me.
Thanks for everyone's input :)
这是 ContextMenu 的默认行为。或者,您可以使用 Popup 类而不是 ContextMenu。然后您可以根据您的要求扩展它。
That is the default behavior of ContextMenu. Alternatively you can use Popup Class instead of ContextMenu. Then you can extend it as your requirement.