Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
i want to show popup window with faded background window wpf

What I have tried:

owner.Opacity = 0.2;
if (showAsDialog)
                            oWindow.ShowDialog();
                        else
                        {
                            oWindow.Show();
                        }
Posted
Updated 17-Jan-19 18:02pm

1 solution

 public void Progress_Close(Window oWindow, Window owner = null)
        {
            try
            {

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Window wndParent = owner;
                    try
                    {
                        if (wndParent == null)
                        {
                            wndParent = Application.Current.MainWindow;
                        }
                        if (wndParent != null)
                        {
                            //if (!WindowHelper.IsOtherWindowsOpen())
                            {
                                wndParent.Opacity = 1;
                                wndParent.Effect = null;
                            }
                        }

                        if (oWindow.Visibility != Visibility.Visible)
                            return;

                        if (!oWindow.IsVisible)
                        {
                            return;
                        }

                        //wndParent = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive); 
                        if (oWindow == null || !oWindow.IsVisible)
                            return;

                        oWindow.Hide();

                    }
                    catch (Exception ex)
                    {
                        //Log.Error("MainWindow \t mnuAbout_Click \t {error}", ex); 
                    }

                }));

            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("Progress_Close:: ", ex);
            }
        }
        public void Progress_Show(Window oWindow, Window owner = null, bool showAsDialog = true)
        {
            try
            {
                if (oWindow == null)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    //Window wndParent = owner; 
                    try
                    {
                        if (owner == null)
                        {
                            owner = Application.Current.MainWindow;
                        }
                        //wndParent = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive); 

                        owner.Opacity = 0.2;
                        owner.Effect = new System.Windows.Media.Effects.BlurEffect();

                        oWindow.ShowInTaskbar = false;
                        oWindow.Owner = owner;
                        oWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                        //if (this.WindowState != WindowState.Maximized) 
                        {
                            oWindow.Left = Application.Current.MainWindow.Left + (this.ActualWidth - oWindow.ActualWidth) / 2;
                            oWindow.Top = Application.Current.MainWindow.Top + (this.ActualHeight - oWindow.ActualHeight) / 2;
                        }
                        if (showAsDialog)
                            oWindow.ShowDialog();
                        else
                        {
                            oWindow.Show();
                        }
                    }
                    catch (Exception ex)
                    {
                        //Log.Error("MainWindow \t mnuAbout_Click \t {error}", ex); 
                    }
                    finally
                    {
                        if (owner != null)
                        {
                            if (showAsDialog && !(owner == Application.Current.MainWindow /*&& IsOtherWindowsOpen()*/))
                            {
                                owner.Opacity = 1;
                                owner.Effect = null;
                                owner.Activate();
                            }
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("Progress_Show:: ", ex);
            }
        }

and call above function like this

private void LoadAboutForm()
        {
            try
            {

                this.Dispatcher.Invoke(new Action(() =>
                {
                    //MainTabControl.SelectedIndex = 0;

                    ofrmAbout = new frmAbout();
                    ofrmAbout.OnCloseWindow += OfrmAbout_OnCloseWindow;
                    Progress_Show(ofrmAbout, Application.Current.MainWindow, true);

                }));
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("MainUserControl.xaml.cs|TabItem_MouseUp()", ex);
            }
        }

        private void OfrmAbout_OnCloseWindow()
        {
            if (ofrmAbout != null)
            {
                Progress_Close(ofrmAbout, Application.Current.MainWindow);
            }
        }
 
Share this answer
 
Comments
Richard Deeming 18-Jan-19 10:15am    
If you want to update your question, then click the green "Improve question" link and edit your question. DO NOT post your update as a "solution".

And the first thing you're going to want to do when you edit your question is to actually ask a question. So far, you've told us what you're trying to do, and shown us some code; but you haven't told us what the problem is, or where you're stuck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900