WPF更改窗口模态状态

时间:2015-02-11 09:27:41

标签: c# wpf dialog window

是否可以在WPF中将窗口从模态更改为非模态?这意味着,我用...ShowDialog();打开窗口,但后来想要切换状态(比如打开窗口...Show();

1 个答案:

答案 0 :(得分:1)

假设您要将窗口切换为' master'窗口,你可以做这样的事情,使得Window1在5秒后变为非模态。

这种方法的缺点是对话框会闪烁。

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();
}

如果你想要从窗口本身将窗口切换到非模态,那么调用Hide()后跟Show()将完成同样的事情(再次使用闪烁)

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

值得注意的是'主人'当您进行此切换时,将调用ShowDialog()的窗口调用。