zoukankan      html  css  js  c++  java
  • wpf Popup 不跟随窗口移动的问题

    首先要写 个 附加依赖项属性

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls.Primitives;
    
    namespace SLWNetWork64Tool.Common
    {
        /// <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 直接绑定窗口就好了

    例如

    <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>

    如果是 用户控件里的话,需要先将popup控件设成public 然后在窗口的构造函数里绑定 窗口 如下

    //设置地址控件的pop弹出框跟随窗口移动
    PopopHelper.SetPopupPlacementTarget(address.Pop_Address, this);

    这个address.Pop_Address 就是Popup,this 呢 就是窗口了

    文章转自https://www.cnblogs.com/zhidanfeng/articles/6882869.html

    略有修改

  • 相关阅读:
    计时器C#
    MySQL Database Command Line Client
    C#小爬虫,通过URL进行模拟发送接收数据
    C#导入导出Excele数据
    正则表达式动态分隔符
    C#中的枚举
    C#中的ToString格式大全
    C# 序列化与反序列化
    C# 对xml进行操作
    时间标签DateTime
  • 原文地址:https://www.cnblogs.com/wuyaxiansheng/p/12660280.html
Copyright © 2011-2022 走看看