解决WPF下popup不随着window一起移动的问题
2024-08-30 09:22:37
/// <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>
最新文章
- CouchDB简介
- Mysql notes
- 收缩SQL数据库日志
- HTML5 图片上传预览
- Asp.Net MVC过滤器小试牛刀
- PID204 / 特种部队
- Oracle—RMAN备份(一)
- Error:Execution failed for task &;#39;:app:dexDebug&;#39;. &;gt; com.android.ide.common.process.ProcessException
- C#中的虚方法和抽象方法(Thirteenth Day)
- chrom 快捷键 整理版
- 每天一个linux命令(29)--Linux chmod命令
- Vue(二十七)当前GitHub上排名前十的热门Vue项目(转载)
- Map<;String,Integer>;acount字符串出现的次数
- Django APP打包重用
- 深入理解java虚拟机---虚拟机工具jps与jstat(十四)
- apache 重点难点
- Python3.7中urllib.urlopen 报错问题
- Dream------Hadoop--网络拓扑与Hadoop--摘抄
- sublime Text与python3的中文编码错误解决办法
- 使用 HTML5 History 新特性增强 Ajax 的体验(转)
热门文章
- Ubuntu 出现access denied by server while mounting
- 记录一次群答问:requests获取cookie
- 洛谷 P1167 刷题
- 原生ajax分页,无刷新分页,最简化。超简单,代码最少
- ESA2GJK1DH1K基础篇: 阿里云物联网平台: 测试云平台显示MQTT客户端发过来的消息
- contest14 CF160div2 oooxx oooxx ooooo
- contest3 CF994 div2 ooxxx? oooox? ooooo?
- [no_perms] Private mode enable, only admin can publish this module
- Gamma展示
- shell (二) shell for循环