2017-10-04 277 views
-1

我想在我的WPF應用程序上實現此功能。我希望在那裏出現彈出窗口(子窗口)的整個屏幕(父窗口)變暗的疊加/背景,因此它將使彈出窗口(子窗口)更具可見性就像下面的圖像一樣。這是一個瀏覽器彈出窗口。然後當彈出窗口(子窗口)關閉時,黑暗的疊加/背景被刪除。 enter image description here如何在wpf中彈出子窗口時模糊父窗口

+0

這個工作非常適合我。 '窗口darkwindow =新窗口() { 背景= Brushes.Black, 不透明度= 0.8, AllowsTransparency =真, WindowStyle = WindowStyle.None, 的WindowState = WindowState.Maximized, 最頂層=真 }; darkwindow.Show(); un.ShowDialog(); darkwindow.Close();' –

回答

1

在推出你的對話框,修改父窗口的效果屬性:

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; 
     } 
    } 
} 
+0

感謝您的迴應。有沒有辦法給模糊添加顏色?它只是模糊了這個元素。 –

+0

已更新示例以顯示彩色分層。 – Eric

相關問題