Search code examples
wpfpopupbrightnessmainwindow

WPF. if pop up window appear, main window brightness decrease //code-behind


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();
        }
...

Solution

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