我们用WPF用的Popup时候会发现,当 StaysOpen=True 的时候,因为Popup不会消失,在父窗口移走的时候Popup仍旧在原地。。。作者在国外网站上无意间发现了这个解决方案,拿出来给大家分享:

方法是为Popup定义一个附加属性。代码如下。

     public class PopopHelper
{
public static DependencyObject GetPopupPlacementTarget(DependencyObject obj)
{
return (DependencyObject)obj.GetValue(PopupPlacementTargetProperty);
} public static void SetPopupPlacementTarget(DependencyObject obj, DependencyObject value)
{
obj.SetValue(PopupPlacementTargetProperty, value);
} // Using a DependencyProperty as the backing store for PopupPlacementTarget. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PopupPlacementTargetProperty =
DependencyProperty.RegisterAttached("PopupPlacementTarget", typeof(DependencyObject), typeof(PopopHelper), new PropertyMetadata(null,OnPopupPlacementTargetChanged)); private static void OnPopupPlacementTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
DependencyObject popupPopupPlacementTarget = e.NewValue as DependencyObject;
Popup pop = d as Popup; Window w = Window.GetWindow(popupPopupPlacementTarget);
if (null != w)
{
w.LocationChanged += delegate
{
var offset = pop.HorizontalOffset;
pop.HorizontalOffset = offset + ;
pop.HorizontalOffset = offset;
};
}
}
} }

之后只需要在Popup控件上这样写即可:

     <Grid>
<TextBox x:Name="placementTextBox"/>
<Popup PopopHelper.PopupPlacementTarget="{Binding ElementName=placementTextBox}" />
</Grid>

本文的示例工程可以从附件下载。

欢迎各种转载,转载请注明来自 Leaco 的博客:http://www.cnblogs.com/Leaco/p/3168540.html

“父窗口拖动的时候Popup不随着父窗口移动”问题的解决方案的更多相关文章

  1. duilib进阶教程 -- 改进窗口拖动 (12)

    现在大家应该都知道caption="0,0,0,32",是指示标题栏区了吧,如果想要整个窗口都能拖动呢? 那直接把高度改成和窗口一样不就得了~O(∩_∩)O~ 嗯,这样是可以,比如 ...

  2. WPF 自定义的窗口拖动

    WPF原有的窗口样式太丑,当我们重新定义窗口时,则需要添加一些额外的功能,如拖动~ 1.在界面上对布局元素如Grid,添加委托事件: MouseLeftButtonDown="UIEleme ...

  3. Duilib改进窗口拖动,使整个窗口都能拖动两种方法(转载)

    转载:http://www.cnblogs.com/XiHua/articles/3490490.html 转载:http://blog.csdn.net/lostspeed/article/deta ...

  4. Winform 窗口拖动

    把窗口边框去掉后,窗口拖动问题: private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否 ...

  5. qml实现窗口拖动

    在去掉窗口标题栏后窗口会失去鼠标拖动效果,所以需要自己添加拖动效果. 实现代码: ApplicationWindow {     id: mainWindow     visible: true    ...

  6. 解决 Popup 位置不随窗口移动更新的问题

    Popup弹出后,因业务需求设置了StaysOpen=true后,移动窗口位置或者改变窗口大小,Popup的位置不会更新. 如何更新位置? 获取当前Popup的Target绑定UserControl所 ...

  7. 29.QT-自定义窗口拖动、自定义QToolButton/QPushButton开关按钮、界面阴影,声音等总结

    自定义窗口及拖动 1.自定义无边框窗口时,需要将窗口标志设为: Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMax ...

  8. WTL中最简单的实现窗口拖动的方法(转)

    目前,很多基于对话框的应用程序中对话框都是不带框架的,也就是说对话框没有标题栏.众所周知,窗口的移动都是通过鼠标拖动窗口的标题栏来实现的,那么现在应用程序中的对话框没有了标题栏,用户如何移动对话框呢? ...

  9. 基于jQuery页面窗口拖动预览效果

    今天给大家分享一款基于Query页面窗口拖动预览效果.这是一款基于jQuery+HTML5实现的模拟页面窗口显示拖动窗口预览特效.这款实例适用浏览器:IE8.360.FireFox.Chrome.Sa ...

随机推荐

  1. [React] React Fundamentals: Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  2. MySQL 权限?

    一.权限表 mysql数据库中的3个权限表:user .db. host 权限表的存取过程是: 1)先从user表中的host. user. password这3个字段中判断连接的IP.用户名.密码是 ...

  3. 适用于cocos2dx的编辑器:Texture,Tilemap,Particle,Action,Level etc

    原文:http://www.cocos2d-x.org/wiki/Editors_for_cocos2d-x_TextureTilemapParticleActionLevel_etc Action ...

  4. .PIG File

    .PIG File File extension: PIG File type: Texture File The PIG file type is primarily associated with ...

  5. svn各种问题总结

    1.out of date 就是说别人已经更新到服务器了,而我修改的还不是服务器最新的. 直接Replace with  资源库的最新内容

  6. HDU1016(bfs)

    import java.util.Scanner;public class Main1016 { public static void main(String[] args) { Scanner ci ...

  7. Android(java)学习笔记149:Android线程形态之 AsyncTask (异步任务)

    1. AsyncTask和Handler的优缺点比较: 1)AsyncTask实现的原理和适用的优缺点        AsyncTask是Android提供的轻量级的异步类,可以直接继承AsyncTa ...

  8. 移动终端学习1:css3 Media Queries简介

    移动终端学习之1:css3 Media Queries简介 1.简介 这篇文章写的不错,我就不重复了,来个链接:http://www.w3cplus.com/content/css3-media-qu ...

  9. Java并发——同步工具类

    CountDownLatch  同步倒数计数器 CountDownLatch是一个同步倒数计数器.CountDownLatch允许一个或多个线程等待其他线程完成操作. CountDownLatch对象 ...

  10. php 中json_decode()和json_encode()的使用方法

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...