WPF 更改 window 模式状态
WPF change window modal state
是否可以在 WPF 中将 window 从模态更改为非模态?这意味着,我用 ...ShowDialog();
打开 window,但后来想切换状态(比如打开 window ...Show();
.
假设您想将 window 从 'master' window 切换为非模态,您可以执行类似这样的操作,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();
}
如果您想将 window 从 window 本身切换到非模态,那么调用 Hide() 后跟 Show() 将完成同样的事情(再次闪烁)
private void SwitchToModelessButton_Click(object sender, RoutedEventArgs e)
{
this.Hide();
this.Show();
}
值得注意的是,当您进行此切换时,对 ShowDialog() 的 'master' windows 调用将 return。
是否可以在 WPF 中将 window 从模态更改为非模态?这意味着,我用 ...ShowDialog();
打开 window,但后来想切换状态(比如打开 window ...Show();
.
假设您想将 window 从 'master' window 切换为非模态,您可以执行类似这样的操作,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();
}
如果您想将 window 从 window 本身切换到非模态,那么调用 Hide() 后跟 Show() 将完成同样的事情(再次闪烁)
private void SwitchToModelessButton_Click(object sender, RoutedEventArgs e)
{
this.Hide();
this.Show();
}
值得注意的是,当您进行此切换时,对 ShowDialog() 的 'master' windows 调用将 return。