为什么WPF弹出窗口在其背景区域被点击时关闭? [英] Why does a WPF Popup close when its background area is clicked?

查看:670
本文介绍了为什么WPF弹出窗口在其背景区域被点击时关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF Popup 控件,其中包含一些编辑控件(列表框,文本框,复选框),并带有相当多的空白。



Popup.StaysOpen 设置为 False ,这是必需的。如果用户在应用程序的其他地方点击,编辑操作应该被视为中止,弹出窗口应该关闭。



不幸的是,弹出窗口也会在用户点击背景区域时关闭弹出窗口(编辑控件之间的空间)。

我试着将弹出窗口设置为 Focusable 。我也尝试设置弹出的孩子(边界)是可以调焦的。没有任何运气。



此外,鼠标事件似乎穿过弹出窗口。弹出下面的任何元素,当我点击它变得焦点。尽管 Popup 边界(我点击它们)都有 IsHitTestVisible Focusable 设置为 true
<解决方案最后,我发现以下工作。给定...

pre $ < Popup x:Name =_ popup
StaysOpen =False
PopupAnimation =Slide
AllowsTransparency =True>

...我在调用 InitializeComponent code> ...

  //确保所有到达
//的鼠标事件弹出被视为已处理,否则弹出将关闭
_popup.MouseDown + =(s,e)=> e.Handled = true;


I have a WPF Popup control that contains some editing controls (list boxes, text boxes, check boxes) laid out with quite a bit of whitespace.

Popup.StaysOpen is set to False, which is required. If the user clicks elsewhere in the application, the editing operation should be considered aborted and the popup should close.

Unfortunately the popup also closes whenever the user clicks within the background region of the popup (the space between the edit controls).

I've tried setting the popup to be Focusable. I've also tried setting the popup's child (a Border) to be focusable. No luck on either front.

Furthermore, the mouse event seems to tunnel through the popup. Whatever element is underneath the popup when I click it becomes focused. This is despite both the Popup and the Border (into which I'm clicking) having both IsHitTestVisible and Focusable set to true.

解决方案

In the end, I found that the following worked. Given...

<Popup x:Name="_popup"
       StaysOpen="False"
       PopupAnimation="Slide"
       AllowsTransparency="True">

...I used this code in the constructor after calling InitializeComponent...

// Ensure that any mouse event that gets through to the
// popup is considered handled, otherwise the popup would close
_popup.MouseDown += (s, e) => e.Handled = true;

这篇关于为什么WPF弹出窗口在其背景区域被点击时关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆