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

略有修改


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM