WPF。如果弹出窗口出现,主窗口亮度降低//代码隐藏

WPF。如果弹出窗口出现,主窗口亮度降低//代码隐藏

问题描述:

我需要如果我的弹出窗口出现(单击后),主窗口亮度必须降低,也许有人知道该怎么做?WPF。如果弹出窗口出现,主窗口亮度降低//代码隐藏

例子: enter image description here

编辑:我创建了画布上,但不知道如何使用它,需要的亮度降低,然后弹出出现。

代码:

private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

      string path1 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader_bg.png"; 
      string path2 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader.gif"; 

      ImageBrush myBrush = new ImageBrush(); 
      Image image = new Image(); 
      image.Source = new BitmapImage(
       new Uri(path1)); 
      myBrush.ImageSource = image.Source; 

      Image ima = new Image(); 
      MediaElement gif = new MediaElement(); 

      ima.Source = new BitmapImage(new Uri(path1)); 
      gif.Source=new Uri(path2); 

      gif.Height = 72; 
      gif.Width = 72; 

      var pop = new Popup 
      { 
       IsOpen = true, 
       StaysOpen = false, 
       AllowsTransparency = true, 
       VerticalOffset = 350, 
       HorizontalOffset = 700, 
       Height = 128, 
       Width = 128, 

      }; 
      Canvas c=new Canvas(); 
      c.Background=Brushes.Black; 
      c.Opacity = 0.6; 


      Grid p = new Grid(); 
      p.Background = myBrush; 

      //p.Children.Add(ima); 
      //p.Children.Add(c); 
      p.Children.Add(gif); 
      pop.Child = p; 


     } 
    } 

编辑2: 我有同样的问题,只是我的代码是变化的。现在我为弹出窗口创建了新的xaml.cs,并尝试达到相同的目的,但我不太一样(我谈论亮度降低)。 它我的新xaml.cs:

namespace uploader 
{ 
    /// <summary> 
    /// Interaction logic for PopupPanel.xaml 
    /// </summary> 
    public partial class PopupPanel : UserControl 
    { 
     private Popup _currentPopup; 
     public PopupPanel() 
     { 
      InitializeComponent(); 

      string path1 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader_bg.png"; 
      string path2 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader.gif"; 

      ImageBrush myBrush = new ImageBrush(); 
      Image image = new Image(); 
      image.Source = new BitmapImage(new Uri(path1)); 
      myBrush.ImageSource = image.Source; 


      MediaElement gif = new MediaElement(); 

      gif.Source=new Uri(path2); 

      gif.Height = 72; 
      gif.Width = 72; 

      _currentPopup = new Popup 
      { 

       StaysOpen = false, 
       AllowsTransparency = true, 
       VerticalOffset = 350, 
       HorizontalOffset = 700, 
       Height = 128, 
       Width = 128, 

      }; 
      Overlay.Visibility = Visibility.Visible; 

      _currentPopup.Closed += PopupClosing; 
      _currentPopup.IsOpen = true; 

      Grid p = new Grid(); 
      p.Background = myBrush; 
      p.Children.Add(gif); 

      _currentPopup.Child = p; 

     } 
     private void PopupClosing(object sender, EventArgs e) 
     { 
      _currentPopup.Closed -= PopupClosing; 
      _currentPopup = null; 

      Overlay.Visibility = Visibility.Collapsed; 
     } 
    } 
} 

我Mainwindow.xaml.cs:

namespace uploader 
{ 

    public partial class MainWindow : Window 
    { 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
     private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      PopupPanel pop = new PopupPanel(); 
     } 
... 

使用您当前的代码,您将需要处理画布叠加。

很容易把它你的XAML中定义如下图所示:

<Window> 
    <Grid> 
     <!--Main content--> 
     <UserControl/> 
     <Grid> 
      <Canvas x:Name="Overlay" 
        Background="Black" 
        Opacity="0.6" 
        Visibility="Collapsed"/> 
      <!--Overlay content--> 
      <UserControl VerticalAlignment="Center" HorizontalAlignment="Center"/> 
     </Grid>   
    </Grid> 
</Window> 

然后,在你的代码隐藏弹出打开之前,你可以设置的知名度,并且当它关闭:

Popup _currentPopup; 

private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ... 

    _currentPopup = new Popup 
    { 
     StaysOpen = false, 
     AllowsTransparency = true, 
     VerticalOffset = 350, 
     HorizontalOffset = 700, 
     Height = 128, 
     Width = 128 
    }; 

    Overlay.Visibility = Visibility.Visible; 

    _currentPopup.Closed += PopupClosing; 
    _currentPopup.IsOpen = true; 

} 

private void PopupClosing(object sender, EventArgs e) 
{ 
    _currentPopup.Closed -= PopupClosing; 
    _currentPopup = null; 

    Overlay.Visibility = Visibility.Collapsed; 
} 

请注意,我正在使用局部变量来保留对弹出窗口的引用。这是我可以从关闭事件取消订阅(有助于防止内存泄漏)

+1

谢谢,很多:) – LTU

+0

我更改我的代码,并且此代码不再工作,我为弹出窗口创建新的xaml.cs ,它会出现,但它不会降低亮度。我编辑我的文章(编辑2),也许你知道如何解决它? – LTU

我用帆布与黑色的背景和不透明度做到这一点在我所有的WPF应用程序

例如:

<Window> 
    <Grid> 
     <!--Main content--> 
     <UserControl/> 
     <Grid> 
      <Canvas Background="Black" Opacity="0.6"/> 
      <!--Overlay content--> 
      <UserControl VerticalAlignment="Center" HorizontalAlignment="Center"/> 
     </Grid>   
    </Grid> 
</Window>