2015-08-03 155 views
0

我需要如果我的彈出窗口出現(單擊後),主窗口亮度必須降低,也許有人知道該怎麼做?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(); 
     } 
... 

回答

0

使用您當前的代碼,您將需要處理畫布疊加。

很容易把它你的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

1

我用帆布與黑色的背景和不透明度做到這一點在我所有的WPF應用程序

例如:

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