0

I need that if my pop up window appear (after click) , the main window brightness has to decrease, maybe someone know how to do it?

Example: enter image description here

EDIT: I create canvas, but do not know how to use it, brightness need decrease then pop up appear.

code:

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;


        }
    }

EDIT 2: I have the same question only my code is change. Now I created new xaml.cs for pop up window, and try to achieve the same purpose, but I do not get the same (I talk about brightness decrease). Its my new 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;
        }
    } 
}

My 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();
        }
...
LTU
  • 195
  • 1
  • 3
  • 18

2 Answers2

1

I do this in all my WPF applications by using a Canvas with black background and opacity

Example:

<Window>
    <Grid>
        <!--Main content-->
        <UserControl/>
        <Grid>
            <Canvas Background="Black" Opacity="0.6"/>
            <!--Overlay content-->
            <UserControl VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>         
    </Grid>
</Window>
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
0

Using your current code, you will need to handle the visibility of the Canvas overlay.

It's easier to to have it defined within your XAML as shown below:

<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>

Then, in your code-behind you can set the visibility before the popup opens, and when it closes:

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

Note, that I am using a local variable to keep a reference to the popup. This is so that I can un-subscribe from the Closing event (helps prevent memory leaks)

d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • I change my code and this code does not work anymore, I create new xaml.cs for popup window, and it appear, but it does'not decrease brightness. I edit my post (EDIT 2) , maybe you know how to fix it? – LTU Aug 04 '15 at 11:58