扩展WPF弹出窗口以模仿StaysOpen行为,但有一些细微差别 [英] Extend WPF popup to mimic StaysOpen behavior with a few nuances

查看:388
本文介绍了扩展WPF弹出窗口以模仿StaysOpen行为,但有一些细微差别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF弹出窗口,它是扩展弹出窗口,因此我可以执行一些自定义行为等.我几乎可以从PopUp的直接属性中获得一种行为.我希望StaysOpen ="false"是我得到的行为,除非失去焦点来自所述弹出窗口的子元素实例化的保存对话框.默认行为会导致对话框立即关闭,因为当它从该父元素窃取焦点时,其父元素会关闭.

有什么想法可以做到这一点吗?我尝试了类似HasFocusChanged的不同事件,但找不到适合我需要更具体地控制此行为的事件.

I have a WPF popup that is an extended popup so I can do some custom behavior etc on. There is one behavior that I can almost almost get from a straight up property on the PopUp. I want StaysOpen="false" to be the behavior I get, except when the loss of focus comes from a save dialog instantiated by a child element of said popup. The default behavior causes the dialog to instantly close because its parent element is closed when it steals focus from that parent element.

Any ideas how I can do this? I have tried different events like HasFocusChanged, but I can''t find one that works like I would need to control this behavior more specifically.

推荐答案

不幸的是,解决方案我终于解决了,没有我想要的那么干净.我喜欢锻炼,所以我以与我给定的解决方案或项目无关的方式扩展了控件.这个是,但是
在其他项目中使用该控件很容易,只需修改几个位置即可.

我的扩展弹出窗口类(之所以称为锚定弹出窗口,是因为在调整窗口大小或移动窗口时,它与其他控件位于相同的相对位置)是在控件模板的内部深层使用的,在这种情况下,将其变为现实是不现实的了解或连接与此相关的命令和操作.结果,我需要在没有任何知识或以编程方式访问与弹出窗口一起使用的ui控件的情况下可以实现的目标.我需要能够扩展弹出窗口类以使其实现.不幸的是,弹出类中可用的事件对此毫无价值.单击弹出窗口的子元素时失去焦点呼叫,但发件人等从不认为自己是弹出窗口的祖先.

在我的项目中,我有一个单例实例,有点像一个应用程序管理器.我的视图模型将调用诸如通过该管理器显示模态之类的东西.我的想法是,我可以使用脚手架对模型进行模态结果等单元测试.事实证明,这也为我提供了答案的路径.

我将事件添加到我的单例管理器中,以通知模态何时打开以及模态刚刚关闭.然后,在弹出窗口类中,我有一个属性,可让我确定给定的弹出窗口是否侦听模式通知.

当弹出窗口打开时,我订阅了这两个事件.当它关闭时,我退订.在模态即将打开事件的处理程序中,我将StaysOpen设置为true,但首先保存了原始的StaysOpen值.如果模态何时关闭,我将保持开放值恢复为保留的值.

这使我可以保持打开状态为false,但仍然可以从弹出窗口的子级调用浏览模式.
Unfortunately, the solution I finally worked out is not as clean as I would like. I like to work out so I extend a control in a way that isn''t specifically tied to my given solution or project. This one is, but it
would be easy to use the control in other projects and just modify a few spots.

My extended pop up class (I call anchored pop up because it stays in the same relative spot to the other controls when a window is re sized or moved) is used deep inside control templates where it wouldn''t be realistic to have it be aware of or connected the commands and actions that matter for this. As a result, I needed something that would be achievable without any knowledge or programmatic access to the ui controls that live with the popup. I needed to be able to extend the popup class to make it happen. Unfortunately the events available in the popup class are worthless for this. Lost focus calls when you click on child elements of the pop up, and yet the senders etc never think they are ancestors of the popup etc.

In my project, I have a singleton instance that is kind of an application manager. My viewmodels will invoke things like showing modals through that manager. The idea is that I can unit test my viewmodels with scaffolding for things like modal results etc. It turned out to also give me the path to my answer.

I added events to my singleton manager to notify when a modal is about to open, and when a modal has just closed. Then in the popup class I have a property that lets me determine if a given popup listens for modal notifications.

When the popup opens, I subscribe to the two events. When it closes I unsubscribe. In the handler for the modal about to open event I set StaysOpen to true, but first save the original StaysOpen value. In the event for when modals close, I restore the stays open value to the preserved value.

That lets me have stays open false, but still have the ability to invoke a browse modal from a child of the popup.


这篇关于扩展WPF弹出窗口以模仿StaysOpen行为,但有一些细微差别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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