解决popup不随着window一起移动的问题
生活随笔
收集整理的這篇文章主要介紹了
解决popup不随着window一起移动的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:解決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
總結
以上是生活随笔為你收集整理的解决popup不随着window一起移动的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python综合学习七之TensorFl
- 下一篇: python3 与 Django 连接数