解决popup不随着window一起移动的问题

简介: 原文:解决popup不随着window一起移动的问题当我们设置Popup的StayOpen="True"时,会发现移动窗体或者改变窗体的Size的时候,Popup并不会跟随着一起移动位置。为了解决这个问题,可以给Popup定义一个附加属性,代码如下所示: /// /// Popup帮助类...
原文: 解决popup不随着window一起移动的问题

当我们设置Popup的StayOpen="True"时,会发现移动窗体或者改变窗体的Size的时候,Popup并不会跟随着一起移动位置。为了解决这个问题,可以给Popup定义一个附加属性,代码如下所示:

/// <summary>
/// Popup帮助类,解决Popup设置StayOpen="True"时,移动窗体或者改变窗体大小时,Popup不随窗体移动的问题
/// </summary>
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);
    }
    
    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)
            {
                //让Popup随着窗体的移动而移动
                w.LocationChanged += delegate
                {
                    var mi = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    mi.Invoke(pop, null);
                };
                //让Popup随着窗体的Size改变而移动位置
                w.SizeChanged += delegate
                {
                    var mi = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    mi.Invoke(pop, null);
                };
            }
        }
    }
}

使用方法:

<Popup x:Name="PART_Popup" AllowsTransparency="True" IsOpen="True" Placement="Bottom"
       PlacementTarget="{Binding ElementName=PART_ToggleButton}"
       HorizontalOffset="-5"
       ZUI:PopopHelper.PopupPlacementTarget="{Binding ElementName=PART_ToggleButton}"
       StaysOpen="True">
    <Border x:Name="ItemsPresenter" Background="Transparent">
        <ItemsPresenter />
    </Border>
</Popup>

参考博客地址:

1、http://www.cnblogs.com/Leaco/p/3168540.html

2、http://www.cnblogs.com/xiaokang088/archive/2011/07/06/2099489.html

目录
相关文章
|
6月前
|
资源调度 前端开发 JavaScript
什么是 bootstrap ngb modal window?
什么是 bootstrap ngb modal window?
49 0
|
C#
C#-ToolTIp和Popup简单使用
很多时候我们需要做一个提示框,来给用户说明这个元素的作用,比如鼠标移动到哪个元素上面,显示一个弹出框并显示这个元素的相关介绍,想到提示内容,我们很容易想到toolip和Popup,接下来就来分别是用一下这两个控件。
208 0
|
JavaScript 前端开发
18、DOM对象(window、screen、location、history、navigation)
18、DOM对象(window、screen、location、history、navigation)
96 0
element-ui:el-dialog遮罩层变黑
element-ui:el-dialog遮罩层变黑
410 0
为什么会有window.window这种设计
为啥要搞这个这个看起来貌似很奇葩的设计。 要解答这个问题,还得请出this,我们经常说浏览器中的全局对象是window, 这句话对了,也还没完全对。 全局对象的真实身份应该是全局作用域的this。 window只是为了便于访问this,弄出来的一个属性。
271 0
为什么会有window.window这种设计
|
编解码
window.innerHeight window.outerHeight 与screen.height
本文目录 1. window 2. innerHeight与outerHeight 3. screen.height 4. 实例
336 0
window.innerHeight window.outerHeight 与screen.height
|
Windows
自定义Window标题栏titleBar
自定义Window标题栏titleBar
301 0
自定义Window标题栏titleBar
WPF listview item mouse enter/over popup
This is because the routing strategy of the Loaded event is Direct, which means that the routed event does not route though an element tree.
956 0