0

Im new in WPF and Im creating a custom dialog (or a message box) in WPF.

My problem is I cant change everything behind my custom dialog to Opacity=0.5. Only the form who called the custom dialog..

I just want to show my dialog box like in Windows 8.1

Anything would be highly appreciated.

TIA!

Jack Frost
  • 318
  • 4
  • 13

2 Answers2

0

Set a property in your MainWindow that controls Opacity of the LayoutRoot. This will change the opacity of your App when the dialog is displayed.

Sample code:

<Grid Name="LayoutRoot" Opacity="{Binding MainWindowOpacity}">
    <StackPanel>
        <Button Click="Button_Click" Content="Click Me to Show Dialog"/>
        <TextBlock Text="WPF" FontSize="72" Margin="50" Foreground="Orange" HorizontalAlignment="Center"/>
    </StackPanel>
</Grid>

and

public partial class MainWindow : Window
{


    public double MainWindowOpacity
    {
        get { return (double)GetValue(MainWindowOpacityProperty); }
        set { SetValue(MainWindowOpacityProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MainWindowOpacity.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MainWindowOpacityProperty =
        DependencyProperty.Register("MainWindowOpacity", typeof(double), typeof(MainWindow), new PropertyMetadata(1.0));


    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //if (MainWindowOpacity < 1) MainWindowOpacity = 1.0;
        //else MainWindowOpacity = 0.5;
        MainWindowOpacity = 0.5;

        // show dialog
        // boilerplate code from http://stackoverflow.com/questions/6417558/modal-dialog-not-showing-on-top-of-other-windows
        Window window = new Window()
        {
            Title = "WPF Modal Dialog",
            ShowInTaskbar = false,               // don't show the dialog on the taskbar
            Topmost = true,                      // ensure we're Always On Top
            ResizeMode = ResizeMode.NoResize,    // remove excess caption bar buttons
            Owner = Application.Current.MainWindow,
            Width = 300,
            Height = 200
        };

        window.ShowDialog();

        MainWindowOpacity = 1.0;
    }
}

and the result:

WPF Modal Dialog with App Opacity Property

Kory Gill
  • 6,993
  • 1
  • 25
  • 33
0

You could add a static method to App.xaml like this:

    public partial class App : Application
{

    public static void SetWindowsToOpacity(double dOpacity, Window ignoreWindow)
    {
        foreach (Window win in System.Windows.Application.Current.Windows)
        {
            if (win != ignoreWindow)
            {
                win.Opacity = dOpacity;
            }

        }
    }
}

Pass in the Dialog window that you do not want to be set to lower opacity and an opacity value for all the rest.

Call the method like this:

        private void button_Click(object sender, RoutedEventArgs e)
    {
        Window1 dialog = new Window1();
        App.SetWindowsToOpacity(0.5, dialog);
        dialog.ShowDialog();
        App.SetWindowsToOpacity(1.0, null);
    }
Kelly Barnard
  • 1,131
  • 6
  • 8