tags:

views:

211

answers:

1

My application has several independent "top-level" windows, which all have completely different functions/workflows.

I am currently using ShowDialog() to make a WPF Window modal. The modal window is a child of one of the main windows. However, it is blocking all the top-level windows once it is open. I would like the dialog to block ONLY the parent window it was launched from. Is this possible?

I'm not sure if it matters, but the window that opens the dialog is the initial window of the app--so all other top-level windows are opened from it.

+1  A: 

One option is to start the windows that you don't want affected by the dialog on a different thread. This may result in other issues for your application, but if those windows do really encapsulate different workflows, that may not be an issue. Here is some sample code I wrote to verify that this works:

<Window x:Class="ModalSample.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="{Binding Identifier}" Height="150" Width="150">
    <StackPanel>
        <TextBox Text="{Binding Identifier}" />
        <Button Content="Open Normal Child" Click="OpenNormal_Click" />
        <Button Content="Open Independent Child" Click="OpenIndependent_Click" />
        <Button Content="Open Modal Child" Click="OpenModal_Click" />
    </StackPanel>
</Window>

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace ModalSample
{
    /// <summary>
    /// Interaction logic for MyWindow.xaml
    /// </summary>
    public partial class MyWindow : INotifyPropertyChanged
    {
        public MyWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private int child = 1;

        private string mIdentifier = "Root";
        public string Identifier
        {
            get { return mIdentifier; }
            set
            {
                if (mIdentifier == value) return;
                mIdentifier = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Identifier"));
            }
        }

        private void OpenNormal_Click(object sender, RoutedEventArgs e)
        {
            var window = new MyWindow {Identifier = Identifier + "-N" + child++};
            window.Show();
        }

        private void OpenIndependent_Click(object sender, RoutedEventArgs e)
        {
            var thread = new Thread(() =>
                {
                    var window = new MyWindow {Identifier = Identifier + "-I" + child++};
                    window.Show();

                    window.Closed += (sender2, e2) => window.Dispatcher.InvokeShutdown();

                    System.Windows.Threading.Dispatcher.Run();
                });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        private void OpenModal_Click(object sender, RoutedEventArgs e)
        {
            var window = new MyWindow { Identifier = Identifier + "-M" + child++ };
            window.ShowDialog();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

I sourced this blog post for running a WPF window on a different thread.

Joseph Sturtevant