在 WPF 中打开模态对话框时的灰色主窗口 [英] Gray main window when open modal dialog in WPF
问题描述
当从主窗口打开模态对话框时,我想用灰色覆盖主窗口.WPF中是否有标准解决方案来模拟这种效果?
When a modal dialog is opened from the main window, I want overlay the main window with gray color. Is there a standard solution in WPF to simulate this effect?
这里有一个例子:
推荐答案
没有内置的功能,但应该很容易实现.
There is no built in functionality for this but it should be easy to implement.
在您的主窗口中,您需要一个没有定义列或行的顶层网格,因此它只是一个占据所有客户区的单元格.Grid 的第一个子元素是一个 UserControl,它实现了应用程序的所有正常内容.第二个孩子只是一个 Rectangle ,它的前景是半透明的灰色.将其可见性数据绑定到主窗口上的适当属性或作为应用程序 DataContext 的 ViewModel 属性.
In your main window you need a Grid at the top level that has no defined columns or rows, so it is just a single cell that takes up all the client area. The first child of the Grid is a UserControl that implements all the normal content of the application. The second child is just a Rectangle with a semi-transparent grey colour as its foreground. Have its Visibility data bound against an appropriate property on your main window or a property of the ViewModel that is the DataContext of your application.
每当你显示一个模态对话框时,你设置适当的属性来显示矩形,当模态对话框被移除时,它会将属性重置回 false.您可以添加一个继承自 Window 的基类,它会自动执行此操作,然后从该基类派生所有实际对话框.
Whenever you show a modal dialog you set the appropriate property to show the rectangle and when the modal dialog is removed it resets the property back to false. You could add a base class that inherits from Window that does this automatically and then derive all your actual dialogs from that base class.
这篇关于在 WPF 中打开模态对话框时的灰色主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!