自定义WPF Popup控件
解决问题
1、WPF Popup 不随着 Window 一起移动的问题
2、WPF Popup 总是显示在最前面
引用命名空间
xmlns:ctrl="clr-namespace:Micro.UI.Controls"
XAML
<ctrl:uiPopup x:Name="canvas" VerticalOffset="-410" IsOpen="True" AllowsTransparency="True" PopupAnimation="Fade">
</ctrl:uiPopup>
C#
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Controls.Primitives; namespace Micro.UI.Controls
{
public class uiPopup : Popup
{
/// <summary>
/// 是否窗口随动,默认为随动(true)
/// </summary>
public bool IsPositionUpdate
{
get { return (bool)GetValue(IsPositionUpdateProperty); }
set { SetValue(IsPositionUpdateProperty, value); }
} public static readonly DependencyProperty IsPositionUpdateProperty =
DependencyProperty.Register("IsPositionUpdate", typeof(bool), typeof(uiPopup), new PropertyMetadata(true, new PropertyChangedCallback(IsPositionUpdateChanged))); private static void IsPositionUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as uiPopup).pup_Loaded(d as uiPopup, null);
} /// <summary>
/// 加载窗口随动事件
/// </summary>
public uiPopup()
{
this.Loaded += pup_Loaded;
} /// <summary>
/// 加载窗口随动事件
/// </summary>
private void pup_Loaded(object sender, RoutedEventArgs e)
{
Popup pup = sender as Popup;
var win = VisualTreeHelper.GetParent(pup);
while (win != null && (win as Window) == null)
{
win = VisualTreeHelper.GetParent(win);
}
if ((win as Window) != null)
{
(win as Window).LocationChanged -= PositionChanged;
(win as Window).SizeChanged -= PositionChanged;
if (IsPositionUpdate)
{
(win as Window).LocationChanged += PositionChanged;
(win as Window).SizeChanged += PositionChanged;
}
}
} /// <summary>
/// 刷新位置
/// </summary>
private void PositionChanged(object sender, EventArgs e)
{
try
{
var method = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (this.IsOpen)
{
method.Invoke(this, null);
}
}
catch
{
return;
}
} //是否最前默认为非最前(false)
public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(Popup), new FrameworkPropertyMetadata(false, OnTopmostChanged));
public bool Topmost
{
get { return (bool)GetValue(TopmostProperty); }
set { SetValue(TopmostProperty, value); }
}
private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
(obj as uiPopup).UpdateWindow();
} /// <summary>
/// 重写拉开方法,置于非最前
/// </summary>
/// <param name="e"></param>
protected override void OnOpened(EventArgs e)
{
UpdateWindow();
} /// <summary>
/// 刷新Popup层级
/// </summary>
private void UpdateWindow()
{
var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
RECT rect;
if (NativeMethods.GetWindowRect(hwnd, out rect))
{
NativeMethods.SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
}
} [StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
#region P/Invoke imports & definitions
public static class NativeMethods
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32", EntryPoint = "SetWindowPos")]
internal static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
}
#endregion
}
}
自定义WPF Popup控件的更多相关文章
- WPF Popup 控件导致被遮挡内容不刷新的原因
WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比 ...
- 示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本
原文:示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本 一.目的:封装了一些控件到自定义的控件库中,方便快速开发 二.实现功能: 基本实现常 ...
- 自定义WPF分页控件
一.分页控件功能说明 实现如上图所示的分页控件,需要实现一下几个功能: 可以设置每页能够展示的最大列数(例如每页8列.每页16列等等). 加载的数组总数量超过设置的每页列数后,需分页展示. 可以直接点 ...
- WPF popup控件的使用
<Window x:Class="WPFPopup.RuntimePopup" xmlns="http://schemas.microsoft.com/wi ...
- 解决wpf popup控件遮挡其他程序的问题
public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
- WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
随机推荐
- ZABBIX监控TCP连接状态
一.获取监控数据 # /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' LISTEN ESTABLISHED T ...
- JavaScript中数组去重汇总
1. 简单的去重方法,利用数组的indexOf下标属性来查询 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ func ...
- 【大数据】0002---MongoDB集群自动分离创建新集群
场景:MongoDB集群运行后,自动产生新的集群 解答:可能没有理解问题,理清思路如下 1.日志分析 2.配置文件检查(日期.版本) 3.网络IP变化 4.github 5.stack overflo ...
- 掌握Visual Studio断点
我知道你现在在想什么.断点?真的吗?关于断点有什么需要掌握的?你按F9然后停在代码行.如果你是这么想的,这篇文章是给你的:继续读 Visual Studio提供了一组相当丰富的断点类型和操作,可以使您 ...
- 从Oop-Klass模型看透反射
<红楼梦>第十二回,贾瑞因痴迷王熙凤,被王熙凤折腾的眼看就快不行了.当然这里面是没有多少爱的,完全因王熙凤的美貌而起.就在这时来了一个跛足道人,带来了一面宝镜,说能治好贾瑞的病.当然这可不 ...
- PATB1024科学计数法
代码是部分正确,只得了13分还有两个测试点没有通过,不知道原因是啥,先不深究了,赶进度. 参考代码: #include<cstdio> #include<cstring> #i ...
- Web项目中使用Log4net 案例
简介: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的日志系统来诊断和修复配置上的问题. 经验表明,日 ...
- PyCharmIDE: 给脚本传递参数
- 一些树上dp的复杂度证明
以下均为内网 树上染色 https://www.lydsy.com/JudgeOnline/problem.php?id=4033 可怜与超市 http://hzoj.com/contest/62/p ...
- 运维(SA)修仙 之路
运维(SA)修仙 之路: 大纲: 系统 ,网络 ,数据库,开发 系统 :linux(cent OS && ubuntu) 网络 :路由,防火墙,安全 数据库:mysql, mong ...