/// <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="popusBottom2" IsOpen="True" local:PopopHelper.PopupPlacementTarget="{Binding ElementName=PCheckBox2}" AllowsTransparency="True" PopupAnimation="Fade" HorizontalOffset="5" VerticalOffset="-420">
  <Image x:Name="panel_preview" Height="100" Width="100"></Image>
</Popup>

  

最新文章

  1. CouchDB简介
  2. Mysql notes
  3. 收缩SQL数据库日志
  4. HTML5 图片上传预览
  5. Asp.Net MVC过滤器小试牛刀
  6. PID204 / 特种部队
  7. Oracle—RMAN备份(一)
  8. Error:Execution failed for task &amp;#39;:app:dexDebug&amp;#39;. &amp;gt; com.android.ide.common.process.ProcessException
  9. C#中的虚方法和抽象方法(Thirteenth Day)
  10. chrom 快捷键 整理版
  11. 每天一个linux命令(29)--Linux chmod命令
  12. Vue(二十七)当前GitHub上排名前十的热门Vue项目(转载)
  13. Map&lt;String,Integer&gt;acount字符串出现的次数
  14. Django APP打包重用
  15. 深入理解java虚拟机---虚拟机工具jps与jstat(十四)
  16. apache 重点难点
  17. Python3.7中urllib.urlopen 报错问题
  18. Dream------Hadoop--网络拓扑与Hadoop--摘抄
  19. sublime Text与python3的中文编码错误解决办法
  20. 使用 HTML5 History 新特性增强 Ajax 的体验(转)

热门文章

  1. Ubuntu 出现access denied by server while mounting
  2. 记录一次群答问:requests获取cookie
  3. 洛谷 P1167 刷题
  4. 原生ajax分页,无刷新分页,最简化。超简单,代码最少
  5. ESA2GJK1DH1K基础篇: 阿里云物联网平台: 测试云平台显示MQTT客户端发过来的消息
  6. contest14 CF160div2 oooxx oooxx ooooo
  7. contest3 CF994 div2 ooxxx? oooox? ooooo?
  8. [no_perms] Private mode enable, only admin can publish this module
  9. Gamma展示
  10. shell (二) shell for循环