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

参考:

http://stackoverflow.com/questions/1600218/how-to-move-a-wpf-popup

http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/2616e607-3954-4bfd-ae29-e0d813263030

 

给窗口加一个“LocationChanged”事件,然后在这个事件中做如下处理:

1 private void window_LocationChanged(object sender, EventArgs e)
2 
3 {
4 
5     this.TestPopup.ClearValue(Popup.IsOpenProperty);
6 
7     this.TestPopup.IsOpen = true;
8 
9 }

 

public MainWindow()
{
 
  
  LocationChanged += new  EventHandler(MainWindow_LocationChanged);
 
  
  }
 
  
 
 
  
void  MainWindow_LocationChanged( object  sender, EventArgs e)
 
  
{
 
  
//参考链接1
 
  
var  mi = typeof (Popup).GetMethod( "UpdatePosition" , System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 
  
mi.Invoke(popBottom, null );
 
  
 
 
  
//此方法容易导致 闪烁
 
  
//popBottom.ClearValue(Popup.IsOpenProperty);
 
  
  //popBottom.IsOpen = true;
 
  
}
 

 方法2:转载于Leaco 的博客http://www.cnblogs.com/Leaco/p/3168540.html

 1 public class PopopHelper
 2     {
 3         public static DependencyObject GetPopupPlacementTarget(DependencyObject obj)
 4         {
 5             return (DependencyObject)obj.GetValue(PopupPlacementTargetProperty);
 6         }
 7 
 8         public static void SetPopupPlacementTarget(DependencyObject obj, DependencyObject value)
 9         {
10             obj.SetValue(PopupPlacementTargetProperty, value);
11         }
12 
13         // Using a DependencyProperty as the backing store for PopupPlacementTarget.  This enables animation, styling, binding, etc...
14         public static readonly DependencyProperty PopupPlacementTargetProperty =
15             DependencyProperty.RegisterAttached("PopupPlacementTarget", typeof(DependencyObject), typeof(PopopHelper), new PropertyMetadata(null,OnPopupPlacementTargetChanged));
16 
17         private static void OnPopupPlacementTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
18         {
19             if (e.NewValue != null)
20             {
21                 DependencyObject popupPopupPlacementTarget = e.NewValue as DependencyObject;
22                 Popup pop = d as Popup;
23 
24                 Window w = Window.GetWindow(popupPopupPlacementTarget);
25                 if (null != w)
26                 {
27                     w.LocationChanged += delegate
28                     {
29                         var offset = pop.HorizontalOffset;
30                         pop.HorizontalOffset = offset + 1;
31                         pop.HorizontalOffset = offset;
32                     };
33                 }
34             }
35         }
36         
37     }

然后在Popup控件上这样写即可:

1 <Grid>
2         <TextBox x:Name="placementTextBox"/>
3        <Popup PopopHelper.PopupPlacementTarget="{Binding ElementName=placementTextBox}" />
4    </Grid>

 

转载于:https://www.cnblogs.com/MarcLiu/p/3688166.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值