解决问题

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控件的更多相关文章

  1. WPF Popup 控件导致被遮挡内容不刷新的原因

    WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比 ...

  2. 示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本

    原文:示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本 一.目的:封装了一些控件到自定义的控件库中,方便快速开发 二.实现功能: 基本实现常 ...

  3. 自定义WPF分页控件

    一.分页控件功能说明 实现如上图所示的分页控件,需要实现一下几个功能: 可以设置每页能够展示的最大列数(例如每页8列.每页16列等等). 加载的数组总数量超过设置的每页列数后,需分页展示. 可以直接点 ...

  4. WPF popup控件的使用

    <Window x:Class="WPFPopup.RuntimePopup"     xmlns="http://schemas.microsoft.com/wi ...

  5. 解决wpf popup控件遮挡其他程序的问题

    public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...

  6. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

  7. 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...

  8. WPF 自定义ComboBox样式,自定义多选控件

    原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...

  9. WPF自定义选择年月控件详解

    本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

随机推荐

  1. Spring Cloud Sleuth + Zipkin 链路监控

    原文:https://blog.csdn.net/hubo_88/article/details/80878632 在微服务系统中,随着业务的发展,系统会变得越来越大,那么各个服务之间的调用关系也就变 ...

  2. svg形状相关的学习(二)

    _ 阅读目录 一:线段 二:笔画特性 1. stroke-width 2. stroke-opacity 3. stroke-dasharray 属性 三:常见的形状 1. 矩形 2. 圆角矩形 3. ...

  3. WebStorm取消默认style样式折叠

    WebStorm取消默认style样式折叠: File--->Settings打开一个窗口 Edit--->CodeFolding 把HTML style attribute的前面的钩去掉 ...

  4. 修改了celery任务老是执行失败,跟shell中调试的结果不同

    因为没有重启celery,没有删除celerybeat-schedule,导致使用的task任务一直是原来缓存的,所以代码一直无法生效,也是日了狗了

  5. C语言中宏的相关知识

    2019/04/27 16:02 1.宏的定义:宏定义就是预处理命令的一种,它允许用一个标识符来表示一个字符串.格式如下: #define name(宏名) stuff(字符串) 本质就是使用宏名去替 ...

  6. 【牛客练习赛53】A-超越学姐爱字符串

    // 题目地址:https://ac.nowcoder.com/acm/contest/1114/A /* 找规律(碰运气) n:1 = 2 n:2 = 3 n:3 = 5 n:4 = 8 ... d ...

  7. mac下编程使用字体

    1.xcode下使用的是Menlo的18号字体 2.webStorm使用的也是Menlo的18号字体 3.sublime Text 使用的也是Menlo的21号字体

  8. JUC锁框架_AbstractQueuedSynchronizer详细分析

      AQS是JUC锁框架中最重要的类,通过它来实现独占锁和共享锁的.本章是对AbstractQueuedSynchronizer源码的完全解析,分为四个部分介绍: CLH队列即同步队列:储存着所有等待 ...

  9. 【Gamma】Scrum Meeting 10

    目录 写在前面 任务进度表 燃尽图 照片 写在前面 例会时间:6.8 22:30-23.00 例会地点:微信群语音通话 代码进度记录github在这里 任务进度表 注:点击链接跳转至相应的issue ...

  10. .netcore使用MimeKit发送邮件

    以163邮箱为例,借助MimeKit nuget安装:MimeKit类库,源码地址:https://github.com/jstedfast/MimeKit 发送方法如下: #region 邮件发送 ...