zoukankan      html  css  js  c++  java
  • 解决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

  • 相关阅读:
    Nginx 解决WebApi跨域二次请求以及Vue单页面问题
    微信小程序部署问题总结
    Webapi文档描述-swagger优化
    [AOP系列]Autofac+Castle实现AOP日志
    WebApi Ajax 跨域请求解决方法(CORS实现)
    MSDTC启用——分布式事务
    [钉钉通知系列]Jenkins发布后自动通知
    [AOP系列]Autofac+Castle实现AOP事务
    [钉钉通知系列]SVN提交后自动推送消息到钉钉群
    Vue H5 History 部署IIS上404问题
  • 原文地址:https://www.cnblogs.com/zhidanfeng/p/6882858.html
Copyright © 2011-2022 走看看