WPF中Popup的几个问题
要用popup控件来解决一些问题。就此带来了一批问题。
问题一、 在popup外任意位置点击时要能关闭popup,这个本来简单,只要加上StaysOpen=false就可以了。但我的popup中有个OpenFileDialog控件,这个控件窗口一打开,popup就被关闭了。
解决:
在btnOpenFile按钮的PreviewMouseLeftButtonDown事件里添加了
((Popup)((FrameworkElement)this.Parent).Parent).StaysOpen = true;
问题二、 StaysOpen设置为True后,点击按钮变成了这样。popup是TopMost的,挡在了文件窗口前面。

解决:
无法简单解决这个问题,只好采用github上的一个NonTopmostPopup自定义控件取代Popup。
为了在关闭文件选择窗口后popup仍然能够在鼠标点击App任意位置时被关闭,在btnOpenFile按钮的Click事件里添加了
((Popup)((FrameworkElement)this.Parent).Parent).StaysOpen = false;
问题三、上述做法的结果是,如果双击文件名时的鼠标位置在popup区域以外,popup还是会直接关闭。因为双击位置的主窗口控件虽然被文件对话框遮挡,但仍会触发MouseLeftButtonUp(或MouseUp)事件,这可能算是文件对话框的一个bug。我猜是因为双击实际上是在第二次点击的MouseDown事件发生后就生效,对话框就关闭了,再往后还有MouseUp事件,却已经处于没有遮挡的裸奔状态了,当然控件们纷纷响应。
没办法,想workaround吧。我的做法是取消按钮Click事件StaysOpen=false,改在主窗口的MouseLeftButtonDown中加下面的代码遍历控件,遇到popup就设置StaysOpen=false。
private void TheShell_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
FindPopupAndClose(TheShell);
} // Enumerate all the descendants of the visual object.
static public void FindPopupAndClose(Visual myVisual)
{
for (int i = ; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
{
// Retrieve child visual at specified index value.
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i); // Do processing of the child visual object.
if (childVisual is Popup)
((Popup)childVisual).StaysOpen = false;
// Enumerate children of the child visual object.
FindPopupAndClose(childVisual);
}
}
问题四、一些情况下,我需要popup StaysOpen=false,并且能跟随主窗口移动而移动(默认情况popup是不动的)。
解决:
参考StackOverFlow上的帖子,在codebehind加下面事件:
public partial class View1 : UserControl
{
// Constructor
public View1()
{
InitializeComponent(); // Window.GetWindow() will return Null if you try to call it here! // Wire up the Loaded handler instead
this.Loaded += new RoutedEventHandler(View1_Loaded);
} /// Provides a way to "dock" the Popup control to the Window
/// so that the popup "sticks" to the window while the window is dragged around.
void View1_Loaded(object sender, RoutedEventArgs e)
{
Window w = Window.GetWindow(popupTarget);
// w should not be Null now!
if (null != w)
{
w.LocationChanged += delegate(object sender2, EventArgs args)
{
var offset = myPopup.HorizontalOffset;
// "bump" the offset to cause the popup to reposition itself
// on its own
myPopup.HorizontalOffset = offset + ;
myPopup.HorizontalOffset = offset;
};
// Also handle the window being resized (so the popup's position stays
// relative to its target element if the target element moves upon
// window resize)
w.SizeChanged += delegate(object sender3, SizeChangedEventArgs e2)
{
var offset = myPopup.HorizontalOffset;
myPopup.HorizontalOffset = offset + ;
myPopup.HorizontalOffset = offset;
};
}
}
}
小小的popup这才算整听话了。
WPF中Popup的几个问题的更多相关文章
- WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书
原文:WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书 最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是 ...
- 关于WPF中Popup控件的小记
在wpf开发中,常需要在鼠标位置处弹出一个“提示框”(在此就以“提示框”代替吧),通过“提示框”进行信息提示或者数据操作,如果仅仅是提示作用,使用ToolTip控件已经足够,但是有些是需要在弹出的框中 ...
- WPF中Popup等弹窗的位置不对(偏左或者偏右)
1.情况如图: 正常情况: 部分特殊情况: 在一般的电脑都能正确显示,就是第一种情况,同样的代码为什么在不同的电脑就会显示不同的位置呢,原来Windows为了满足 不同需求的用户,左撇 ...
- 关于wpf中popup跟随鼠标移动显示
最近在做一个画图工具,里面有一个功能是需要实现,当鼠标移动的时候在,鼠标的旁边显示坐标信息. 第一反应是想到了tooltip,但是tooltip有许多的限制,查询资料的过程中看到了popup,popu ...
- WPF中Popup上的textbox无法切换到中文输入法
As Marco Zhou has said in the msdn forum (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b ...
- 关于WPF中Popup中的一些用法的总结
Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧: System.Object ...
- WPF中Popup控件的使用
一.Popup控件的主要属性 Popup表示具有内容的弹出窗口,其主要属性为: Child:获取或设置 Popup控件的内容. IsOpen:获取或设置一个值,该值指示Popup 是否可见 Place ...
- 让WPF的Popup不总置顶的解决方案
使用WPF的Popup的时候会发现有一个问题,它总是会置顶,只要Popup的StayOpen不设置为False,它就一直呆在最顶端,挡住其他的窗口. 解决方案是继承Popup重新定义控件PopupEx ...
- wpf之Popup弹出自定义输入"键盘"
在很多工厂的信息化MES系统中,车间的采集数据的机器是触摸屏电脑(工厂环境所限,用外接鼠标键盘反而不方便). 由于没有外接键盘,所以用户无法像坐在办公室一样,用鼠标键盘进行录入信息. 这时我们可以用w ...
随机推荐
- PHP操作字符串 截取指定长度字符 移除字符串两侧 左侧 右侧指定字符 或空白字符 替换字符
trim() trim() 函数移除字符串两侧的空白字符或其他预定义字符. <?php $str = "Hello World!"; echo $str . "&l ...
- CentOS7下安装chrome浏览器
在CentOS 7环境下安装chrome浏览器 1.修改yum源 在/etc/yum.repos.d/目录下新建文件google-chrome.repo,向其中添加如下内容: [google-chro ...
- JS正则表达式将url转成json格式
var url = location.search.substr(1); param = {}; console.log(url); url.replace(/([^?&]+)=([^?&am ...
- Daily Scrum02 12.07
最近大家都在赶编译的大作业,没日没夜的码代码,调试,大家都很辛苦,但是,我们团队的工作,大家也不能懈怠啊! 大家要顶住压力,加油努力啊! Member 任务进度 下一步工作 吴文会 就总结点进行汇报 ...
- Learn ZYNQ (7)
矩阵相乘的例子 参考博客:http://blog.csdn.net/kkk584520/article/details/18812321 MatrixMultiply.c typedef int da ...
- matlab处理图像代码
1.图像的读取MATLAB中从图像文件中读取数据用函数imread(),这个函数的作用就是将图像文件的数据读入矩阵中,此外还可以用imfinfo()函数查看图像文件的信息(见例1)%例1:图像数据及图 ...
- iOS Crash日志
Understanding Crash Reports on iPhone OS https://developer.apple.com/videos/wwdc/2010/?id=317 http:/ ...
- 在 Linux 下搭建 Git 服务器
环境: 服务器 CentOS6.6 + git(version 1.7.1)客户端 Windows10 + git(version 2.8.4.windows.1) ① 安装 Git Linux 做为 ...
- ajax返回json在 IE下,提示打开或保存的解决方法
Content-type要设置成 text/html 我是用的mvc jquery.form.js 提交的表单. 返回json响应数据. 结果在ie下提示打开或保存,查看保存的内容.就是我返回的jso ...
- golang 环境一键式配置
在window下,通过以下bat,自动设置环境变量,启动终端,并cd到gopath目录 set goroot=c:\go set gopath=d:\go @start "start gol ...