Modal state of a WPF change window

Is it possible in WPF to change a window from modal to modeless? This means that I open the window with ...ShowDialog();, but later I want to switch the state (for example, open the window ...Show();.

+4
source share
1 answer

Assuming you want to switch the window to a modeless one from the "master" window, you can do something like this, making Window1 become modeless after 5 seconds.

The disadvantage of this approach is that the dialogue will flicker.

private Window1 myWindow = new Window1();

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    // Using a timer to simulate something happening 5 seconds later that would cause dialog state to change
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
    dispatcherTimer.Start();
    // The following line will block until you switch the dialog from modal to non-modal
    myWindow.ShowDialog();            
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    (sender as DispatcherTimer).Stop();
    myWindow.Hide();
    myWindow.Show();
}

If you want to switch the window to a modeless one from the window itself, then calling Hide (), followed by Show (), will do the same thing (again with flickering)

private void SwitchToModelessButton_Click(object sender, RoutedEventArgs e)
{
    this.Hide();
    this.Show();
}

, 'master', ShowDialog(), , .

+1

All Articles