WPF和Prism视图叠加 [英] WPF and Prism View Overlay

查看:301
本文介绍了WPF和Prism视图叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些使用棱镜框架覆盖视图的帮助,它比这要复杂一些,所以让我解释一下.

我有一个shell(wpf窗口),并且我在一个模块中有2个视图(A和B-两个用户控件). 当外壳加载时,它加载视图A.在视图A上,我有一个弹出"视图B的按钮 用于一些用户输入.所以我自然会想到某种模式窗口/控件,甚至弹出窗口.但是我面对弹出式窗口的问题是,当我移动外壳时,弹出式窗口保持固定,并且不会阻止视图A中的事件.我尝试禁用视图A以停止触发事件,并且我也尝试使用a使视图B与外壳一起移动.只有画布可以工作,但是我现在需要一种方法来阻止它.无论如何,我可以用棱镜在另一个视图之上叠加一个视图吗?或其他人如何使用棱镜&来创建模式弹出窗口WPF?任何建议或指示,将不胜感激.

解决方案

如果要在没有额外窗口的情况下使用嵌入式对话框,则可以使用Prism的RegionManager来实现概述的行为.技巧是使PopUp区域与可视树中的主要区域平行:

<Grid>
   <ContentControl cal:RegionManager.RegionName="MainRegion" IsEnabled={Binding IsNoPopUpActive} />
   <ContentControl cal:RegionManager.RegionName="PopUpRegion"/>
</Grid>

现在使用RegionManager将视图"A"放入"MainRegion".创建类似于IPopUpDialogController的控制器类.它应负责按需将视图"B"(或应用程序中的任何其他PopUpView)放入"PopUpRegion".另外,它还应控制一个标志,该标志指示要启用或禁用基础"MainRegion".这样,在弹出窗口关闭之前,用户将无法使用视图"A"中的控件.

甚至可以在将框架推入Dispatcher之前使用ComponentDispatcher.PushModal()以模态方式完成此操作.但是,我建议避免使用模式对话框.


更新:根据注释中的要求,可以在后视图模型中实现IsNoPopUpActive.在那里,您可以将其链接到弹出区域的RegionManager的View集合:

public bool IsNoPopUpActive 
{ 
    get { return _regionManager.Regions["PopUpRegion"].Views.Count() == 0; }
}

请记住,一旦修改视图集合(添加/删除弹出窗口),便立即触发PropertyChanged事件.

仅供参考:如今,我避免在后台禁用控件,而是插入透明面板.这样可以避免单击后台控件.但是,这不能处理键盘输入(按Tab键进入控件).要修复键盘输入,您需要确保弹出窗口中的键盘焦点被捕获(

I need some help with overlaying views using the prism framework.Its a little more complexed than that so let me explain.I could be over-thinking this as well :D

i have shell (wpf window) and i have 2 views(A & B - both usercontrols) in a module. when the shell loads it loads view A. On view A i have a button to "popup" view B for some user input. so naturally i would think to some sort of modal window/control, maybe even a popup. however the problem i face with the popup is that when i move the shell the popup remains fixed and it doesnt block events in view A. I've tried disabling view A to stop events being fired and i've also tried to use a to get the view B move with the shell. Only the canvas works but i now need a way to block it tho'. Is there anyway i can overlay a view on top of another view with prism? or how does everyone else create modal popups with prism & wpf? any advise or pointers would be greatly appreciated.

解决方案

If you want to use embedded dialogs without an extra window, you can use Prism's RegionManager to achieve the outlined behavior. The trick is to put the PopUp region parallel to your main region in the visual tree:

<Grid>
   <ContentControl cal:RegionManager.RegionName="MainRegion" IsEnabled={Binding IsNoPopUpActive} />
   <ContentControl cal:RegionManager.RegionName="PopUpRegion"/>
</Grid>

Now use the RegionManager to put view "A" into the "MainRegion". Create a controller class similar to IPopUpDialogController. It should be responsible for putting your view "B" (or any other PopUpView in your application) into the "PopUpRegion" on demand. Addtionally, it should control a flag that signal the underlying "MainRegion" to be enabled or disabled. This way a user won't be able to play with the controls in your view "A" until the pop up is closed.

This can even be done in a modal fashion by using ComponentDispatcher.PushModal() before pushing a frame onto the Dispatcher. However, I would recommend avoid modal dialogs.


Update: As requested in a comment, the IsNoPopUpActive could be implemented in the backing view model. There you could link it to RegionManager's View collection for the popup region:

public bool IsNoPopUpActive 
{ 
    get { return _regionManager.Regions["PopUpRegion"].Views.Count() == 0; }
}

Remember to trigger a PropertyChanged event as soon as you modify the views collection (add/remove a popup).

Just for your information: nowadays I avoid disabling the controls in the background and instead insert a transparent panel. This avoids clicking on background controls. However, this does not handle keyboard input (tab-ing to controls). To fix the keyboard input you need to make sure that the keyboard focus is trapped in the popup (MSDN on WPF Focus concepts).

Adding the following focus attributes to the popup region should do the trick:

KeyboardNavigation.DirectionalNavigation="None"
KeyboardNavigation.ControlTabNavigation="None"
KeyboardNavigation.TabNavigation="Cycle"
KeyboardNavigation.TabIndex="-1"

这篇关于WPF和Prism视图叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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