【问题标题】:how to close a WPF Dialog Window when the user clicks outside it当用户在其外部单击时如何关闭 WPF 对话框窗口
【发布时间】:2015-04-29 14:44:43
【问题描述】:

如果用户单击 System.Windows.Window 外部的任何位置,我想关闭它。窗口显示为对话框。如何接收 wpf 窗口外的点击?

AppointmentDetailsView.ShowDialog();

【问题讨论】:

  • 作为用户我不喜欢这种行为 ;)
  • @blindmeis 显然你还没有遇到过用例。检查 Metrotwit。

标签: wpf


【解决方案1】:

这个怎么样?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnDeactivated(EventArgs e)
    {
        base.OnDeactivated(e);
        Close();
    }
}

【讨论】:

  • 这是一个更简单的解决方案,并且可以正常工作。您也可以在 XAML 或代码中将事件处理程序添加到 Deactivated 事件:'Deactivated="Window_Deactivated"' 或 'Deactivated += Window_Deactivated;' /* 在代码中 */ 分别。然后只需调用 close();在处理程序中。
  • 这有一个问题;如果用户按下右上角的X,窗口将关闭,关闭操作会产生异常。
猜你喜欢
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 2023-04-06
  • 2016-07-16
  • 2016-01-21
相关资源
最近更新 更多