如果用户点击它以外的任何地方,我想关闭一个System.Windows.Window。窗口显示为对话框。如何在wpf窗口外收到点击次数?
AppointmentDetailsView.ShowDialog();
答案 0 :(得分:17)
这个怎么样?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
Close();
}
}
答案 1 :(得分:2)
经过进一步探索,我找到了一个运作良好的解决方案。希望有人会发现它很有用:
RadWindow PopupWindow;
public SampleView()
{
InitializeComponent();
PopupWindow = new RadWindow { Header = "Details", Name = "radWindow" };
PopupWindow.Loaded += new RoutedEventHandler(PopupWindow_Loaded);
}
void PopupWindow_Loaded(object sender, RoutedEventArgs e)
{
Canvas canvas = Framework.Traversal.FirstVisualAncestorOfType<Canvas>(PopupWindow);
canvas.MouseLeftButtonDown -= canvas_MouseLeftButtonDown;
canvas.MouseLeftButtonDown += canvas_MouseLeftButtonDown;
}
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point mousepoint = e.GetPosition(null);
var elements = VisualTreeHelper.FindElementsInHostCoordinates(mousepoint, PopupWindow);
if (elements.Count() == 0)
PopupWindow.Close();
}