none
How to grey out (disable) the parent window

    Question

  • If you have a modal dialog (window.ShowDialog();) box is there an easy way to make the parent turn gray while the dialog is opened?

    Thanks,

    Rhonda

    Wednesday, March 17, 2010 10:37 PM

Answers

  • Hi Skydolphin,

    >How to change the opacity. 

    You can change the opacity when you are creating the Adorner. It may be  like this:

            protected override void OnRender(DrawingContext drawingContext)

            {

                SolidColorBrush sr=new SolidColorBrush();

                sr.Color = Colors.Black;

                sr.Opacity=0.5;//change the opacity here

                drawingContext.DrawRectangle(sr,null, new Rect(new Point(0, 0), DesiredSize));

                base.OnRender(drawingContext);

            }

    >When I click Cancel or Submit on the popup, how do I return to my normal screen.  Currently the solid block stays there.

    You have posted  2 methods to us ,but it seems that there are some errors.;

    >parentAdorner.Remove(new UMAdorner(mainPane));

    This code will not work because you are removing a UMAdorner which does not exisit in the window.It has been created jusu now.To fix this ,you should declare a field in you class and create a UMAdorner instance before you use it like this:

    SimpleCircleAdorner sim;

            public Window8()

            {

                InitializeComponent();

                sim = new SimpleCircleAdorner(mainPane);//create it

               }

     

    Then you can write the code like this:

     

            private void AttachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Add(sim);

            }

     

            private void DettachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Remove(sim);

              }

     Now I have a sample for you to refer on the whole:

     

    markup:

    <Window x:Class="MSDN.Window8"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window8" Height="300" Width="300">

        <Grid Name="mainPane">

            <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="myTextBox" VerticalAlignment="Top" Width="120" />

            <Button Height="23" HorizontalAlignment="Left" Margin="29,69,0,0" Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click">New Window</Button>

        </Grid>

    </Window>

    Code behind:

    public partial class Window8 : Window

        {

            SimpleCircleAdorner sim;

            public Window8()

            {

                InitializeComponent();

                sim = new SimpleCircleAdorner(mainPane);

            }

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                Window9 win9 = new Window9(this);

                win9.ShowDialog();

            }

     

            private void AttachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Add(sim);

            }

     

            private void DettachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Remove(sim);

            }

     

            private bool isShowed;

            public bool IsShowed

            {

                get

                {

                    return isShowed;

                }

                set

                {

                    if (isShowed != value)

                    {

                        isShowed = value;

                        if (isShowed == true)

                        {

                            AttachAdorner();

                        }

                        else

                        {

                            DettachAdorner();

                        }

                    }

                }

            }

     

        }

     

        public class SimpleCircleAdorner : Adorner

        {

            public SimpleCircleAdorner(UIElement adornedElement)

                : base(adornedElement)

            {

            }

     

            protected override void OnRender(DrawingContext drawingContext)

            {

                SolidColorBrush sr=new SolidColorBrush();

                sr.Color = Colors.Black;

                sr.Opacity=0.5;

                drawingContext.DrawRectangle(sr,null, new Rect(new Point(0, 0), DesiredSize));

                base.OnRender(drawingContext);

            }

        } 

    Window 9:

     

    Markup:

     

    <Window x:Class="MSDN.Window9"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window9" Height="300" Width="300">

        <Grid>

            <Button Margin="82,0,121,101" Name="button1" Click="button1_Click" Height="23" VerticalAlignment="Bottom">OK</Button>

        </Grid>

    </Window>

     

    Code behind:

        public partial class Window9 : Window

        {

            Window8 window8;

            public Window9()

            {

                InitializeComponent();

            }

     

            public Window9(Window8 window8)

            {

                InitializeComponent();

                this.window8 = window8;

                this.window8.IsShowed = true;

            }

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                this.window8.IsShowed = false;

                this.Close();

            }

    }

     

    If you have any question,please feel free to let me know.

    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Monday, March 22, 2010 6:15 AM
  • Before you call window.ShowDialog, make sure to set the window's Owner to the parent window.

    This won't make the parent "turn grey", but it will set the child's position appropriately, and make it so, if a user clicks in the parent window, the dialog is raised in front.

    This is the standard way of handling dialogs, and preventing confusion from having the original "owner" window displayed.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Wednesday, March 17, 2010 10:44 PM
    Moderator
  • Rhonda,
    In addition to what Reed has suggested, you can still disable the parent Window. Here is code behind in a Window with a Button on it.

    private void btnModal_Click(object sender, RoutedEventArgs e)
    {
        this.IsEnabled = false;
        MessageBox.Show("Here is the modal message");
        this.IsEnabled = true;
    }

    'this' refers to the parent Window itself. Just before showing the modal, disable the parent, then enable it on return. You can use your ShowDialog() instead of the MessageBox.


    Hope this helps.
    noorbakhsh حميد نوربخش
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Wednesday, March 17, 2010 10:54 PM
  • Hi Rhonda,

    I'd approach this with an Adorner. Take a look at this blog where the author shows how to provide an overlay that allows to do what I suppose you're after.
    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:40 AM
    Thursday, March 18, 2010 4:40 PM

All replies

  • Before you call window.ShowDialog, make sure to set the window's Owner to the parent window.

    This won't make the parent "turn grey", but it will set the child's position appropriately, and make it so, if a user clicks in the parent window, the dialog is raised in front.

    This is the standard way of handling dialogs, and preventing confusion from having the original "owner" window displayed.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Wednesday, March 17, 2010 10:44 PM
    Moderator
  • Rhonda,
    In addition to what Reed has suggested, you can still disable the parent Window. Here is code behind in a Window with a Button on it.

    private void btnModal_Click(object sender, RoutedEventArgs e)
    {
        this.IsEnabled = false;
        MessageBox.Show("Here is the modal message");
        this.IsEnabled = true;
    }

    'this' refers to the parent Window itself. Just before showing the modal, disable the parent, then enable it on return. You can use your ShowDialog() instead of the MessageBox.


    Hope this helps.
    noorbakhsh حميد نوربخش
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Wednesday, March 17, 2010 10:54 PM
  • Thanks guys,

    That is getting really close to what I need.  The parent window is correct but now I also need to disable the page that inside the parent window.  The modal dialog is getting called from the Page code.

    Rhonda
    Wednesday, March 17, 2010 11:10 PM
  • Hi Rhonda,

    I'd approach this with an Adorner. Take a look at this blog where the author shows how to provide an overlay that allows to do what I suppose you're after.
    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Linda Liu Monday, March 22, 2010 9:40 AM
    Thursday, March 18, 2010 4:40 PM
  • Thank Olaf,

    I implemented something similar to what was in the blog.  Two things I can't figure out. 

    1)  How to change the opacity. 

    2)  When I click Cancel or Submit on the popup, how do I return to my normal screen.  Currently the solid block stays there.

     

    private void AttachAdorner()
            {
                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);
                parentAdorner.Add(new UMAdorner(mainPane));
            }
    
            private void DettachAdorner()
            {
                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);
                parentAdorner.Remove(new UMAdorner(mainPane));
            }

     

    Any ideas?

    Thanks, Rhonda

     

    Friday, March 19, 2010 11:09 PM
  • Hi Skydolphin,

    >How to change the opacity. 

    You can change the opacity when you are creating the Adorner. It may be  like this:

            protected override void OnRender(DrawingContext drawingContext)

            {

                SolidColorBrush sr=new SolidColorBrush();

                sr.Color = Colors.Black;

                sr.Opacity=0.5;//change the opacity here

                drawingContext.DrawRectangle(sr,null, new Rect(new Point(0, 0), DesiredSize));

                base.OnRender(drawingContext);

            }

    >When I click Cancel or Submit on the popup, how do I return to my normal screen.  Currently the solid block stays there.

    You have posted  2 methods to us ,but it seems that there are some errors.;

    >parentAdorner.Remove(new UMAdorner(mainPane));

    This code will not work because you are removing a UMAdorner which does not exisit in the window.It has been created jusu now.To fix this ,you should declare a field in you class and create a UMAdorner instance before you use it like this:

    SimpleCircleAdorner sim;

            public Window8()

            {

                InitializeComponent();

                sim = new SimpleCircleAdorner(mainPane);//create it

               }

     

    Then you can write the code like this:

     

            private void AttachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Add(sim);

            }

     

            private void DettachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Remove(sim);

              }

     Now I have a sample for you to refer on the whole:

     

    markup:

    <Window x:Class="MSDN.Window8"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window8" Height="300" Width="300">

        <Grid Name="mainPane">

            <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="myTextBox" VerticalAlignment="Top" Width="120" />

            <Button Height="23" HorizontalAlignment="Left" Margin="29,69,0,0" Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click">New Window</Button>

        </Grid>

    </Window>

    Code behind:

    public partial class Window8 : Window

        {

            SimpleCircleAdorner sim;

            public Window8()

            {

                InitializeComponent();

                sim = new SimpleCircleAdorner(mainPane);

            }

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                Window9 win9 = new Window9(this);

                win9.ShowDialog();

            }

     

            private void AttachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Add(sim);

            }

     

            private void DettachAdorner()

            {

                AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(mainPane);

                parentAdorner.Remove(sim);

            }

     

            private bool isShowed;

            public bool IsShowed

            {

                get

                {

                    return isShowed;

                }

                set

                {

                    if (isShowed != value)

                    {

                        isShowed = value;

                        if (isShowed == true)

                        {

                            AttachAdorner();

                        }

                        else

                        {

                            DettachAdorner();

                        }

                    }

                }

            }

     

        }

     

        public class SimpleCircleAdorner : Adorner

        {

            public SimpleCircleAdorner(UIElement adornedElement)

                : base(adornedElement)

            {

            }

     

            protected override void OnRender(DrawingContext drawingContext)

            {

                SolidColorBrush sr=new SolidColorBrush();

                sr.Color = Colors.Black;

                sr.Opacity=0.5;

                drawingContext.DrawRectangle(sr,null, new Rect(new Point(0, 0), DesiredSize));

                base.OnRender(drawingContext);

            }

        } 

    Window 9:

     

    Markup:

     

    <Window x:Class="MSDN.Window9"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window9" Height="300" Width="300">

        <Grid>

            <Button Margin="82,0,121,101" Name="button1" Click="button1_Click" Height="23" VerticalAlignment="Bottom">OK</Button>

        </Grid>

    </Window>

     

    Code behind:

        public partial class Window9 : Window

        {

            Window8 window8;

            public Window9()

            {

                InitializeComponent();

            }

     

            public Window9(Window8 window8)

            {

                InitializeComponent();

                this.window8 = window8;

                this.window8.IsShowed = true;

            }

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                this.window8.IsShowed = false;

                this.Close();

            }

    }

     

    If you have any question,please feel free to let me know.

    • Marked as answer by Linda Liu Monday, March 22, 2010 9:39 AM
    Monday, March 22, 2010 6:15 AM
  • Thanks,

    I managed to get the opacity figured out right before I left on Friday and all I have to say about the part is "Der!"  I don't know why I didn't see that.  It works perfectly now. 

    Thanks for you reply. 

    Rhonda

     

     

     

    Monday, March 22, 2010 3:30 PM
  • Would an adorner also be a good way to create a dropshadow on the window?  We have a borderless window that we created using a custom chrome but we also need to add a dropshadow.  It seems like an adorner would work for that as well.  Does anyone have any examples?

    Thanks,

    Rhonda

    Tuesday, March 23, 2010 4:35 PM
  • Hi Rhonda,

    what problem did you have with a chrome? Here's a little sample (note that you need a reference to PresentationFramework.Aero):

    <Window x:Class="WpfTests.WindowWithShadow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
            WindowStyle="None" 
            WindowStartupLocation="CenterScreen"
            Background="Transparent"
            AllowsTransparency="True"
            Title="WindowWithShadow" 
            Height="300" Width="300">
       <Window.Resources>
          <Color x:Key="ShadowColor">#887D955B</Color>
          <LinearGradientBrush x:Key="SplashBackgroundBrush" StartPoint="0,0" EndPoint="1,1">
             <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0.0" Color="#FFC6E798"/>
                <GradientStop Offset="1.0" Color="#FFEBF7DB"/>
             </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>
       </Window.Resources>
       
       <mwt:SystemDropShadowChrome 
                Color="{StaticResource ShadowColor}" 
                CornerRadius="1"
                Margin="0,0,3,3">
          <Grid Background="{StaticResource SplashBackgroundBrush}" Margin="0,0,3,3">
    
          </Grid>
       </mwt:SystemDropShadowChrome>
    </Window>
    


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Tuesday, March 23, 2010 4:49 PM
  • Not really having a problem with the chrome.  I just wasn't sure what was the best way to do a dropshadow.  Thanks for the sample. 

    Rhonda

     

    Tuesday, March 23, 2010 5:27 PM
  • Olaf,

    Our designer wants the shadow all the way around the window not just on the bottom and right like a typical dropshadow.  Any ideas?

     

    Rhonda

     

    Tuesday, March 23, 2010 5:34 PM
  • Olaf,

    Our designer wants the shadow all the way around the window not just on the bottom and right like a typical dropshadow.  Any ideas?

     

    Rhonda

     

    Tuesday, March 23, 2010 5:34 PM
  • Hi Rhonda,

    hmm. You could try with a DropShadowEffect. Something along the line of:

    <Window x:Class="WpfTests.WindowWithShadow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowStyle="None" 
            WindowStartupLocation="CenterScreen"
            Background="Transparent"
            AllowsTransparency="True"
            Title="WindowWithShadow" 
            Height="300" Width="300">
       <Window.Resources>
          <Color x:Key="ShadowColor">#887D955B</Color>
          <LinearGradientBrush x:Key="SplashBackgroundBrush" StartPoint="0,0" EndPoint="1,1">
             <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0.0" Color="#FFC6E798"/>
                <GradientStop Offset="1.0" Color="#FFEBF7DB"/>
             </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>
       </Window.Resources>
       <Window.Effect>
          <DropShadowEffect ShadowDepth="0" Color="{StaticResource ShadowColor}" BlurRadius="20"/>
       </Window.Effect>
       <Grid Background="{StaticResource SplashBackgroundBrush}" Margin="15">
    
       </Grid>
    </Window>
    
    You'll probably have to play with BlurRadius, margins and padding if you want the shadow to stick out better.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Tuesday, March 23, 2010 5:52 PM
  • That seems to work quite well.  Thanks for all your help Olaf.

     

    Rhonda

    Tuesday, March 23, 2010 6:09 PM