如何在wpf中弹出子窗口时模糊父窗口

时间:2017-10-04 15:36:22

标签: c# wpf popup

我想在我的WPF应用上实现此功能。我希望有一个像整个屏幕(父窗口)的黑暗覆盖/背景,弹出窗口(子窗口)发生,这将给弹出窗口(子窗口)更多的可见性就像下面的图像。它是一个浏览器弹出窗口。然后,当弹出窗口(子窗口)关闭时,黑暗的叠加/背景被移除。enter image description here

1 个答案:

答案 0 :(得分:2)

在启动对话框之前,修改父窗口的Effect属性:

parentWindow.Effect = new BlurEffect();

对话框关闭时:

parentWindow.Effect = null;

为了向叠加层添加颜色,您可以在图层中工作(为简单起见,我将使用代码隐藏方法;如果您有时间,请转到MVVM /行为):

XAML:

<Window x:Class="WpfApp3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp3"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Grid">
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label>Label</Label>
        <TextBox Grid.Row="1"></TextBox>
        <Button Click="ButtonBase_OnClick">Click</Button>
    </Grid>
    <Border x:Name="Splash" Grid.RowSpan="4" Opacity=".2" Visibility="Collapsed" Background="Black">
    </Border>
</Grid>
</Window>

代码:

using System.Windows;
using System.Windows.Media.Effects;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Grid.Effect = new BlurEffect();
            Splash.Visibility = Visibility.Visible;

            var dlg = new Window();

            dlg.ShowDialog();

            Splash.Visibility = Visibility.Collapsed;
            Grid.Effect = null;
        }
    }
}