Gray main window when open modal dialog in WPF

落花浮王杯 提交于 2020-05-13 08:54:45

问题


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?

Here one example:


回答1:


There is no built in functionality for this but it should be easy to implement.

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.

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.




回答2:


As with all XAML, there are a million ways to skin this cat, but here is one example:

Dialog.xaml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        WindowStyle="None"
        AllowsTransparency="True"
        WindowStartupLocation="CenterOwner" 
        WindowState="Maximized"
        Background="#33000000">

    <Grid Width="323" Height="200" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#FFFFFF">
        <!-- grid things go here -->
    </Grid>
</Window>

This will open full screen, and cover the original application with a grey mask. The contents of the dialog will be in a centred grid with a white background.



来源:https://stackoverflow.com/questions/28487729/gray-main-window-when-open-modal-dialog-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!