WPF 已知问题 Popup 吃掉 PreviewMouseDown 事件

在 WPF 中,使用 Popup 也许会看到 PreviewMouseDown 事件被吃掉

因为 PreviewMouseDown 是 RoutingStrategy.Direct 路由事件,不能在多个视觉树使用,在设置 Popup 点击界面 StaysOpen=”False” 的逻辑就在下面代码

private void OnPreviewMouseButton(MouseButtonEventArgs e)
{
    // We should only react to mouse buttons if we are in an auto close mode (where we have capture)
    if (_cacheValid[(int)CacheBits.CaptureEngaged] && !StaysOpen)
    {
        Debug.Assert( Mouse.Captured == _popupRoot.Value, "_cacheValid[(int)CacheBits.CaptureEngaged] == true but Mouse.Captured != _popupRoot");

        // If we got a mouse press/release and the mouse isn't on the popup (popup root), dismiss.

        // When captured to subtree, source will be the captured element for events outside the popup.

        if (_popupRoot.Value != null && e.OriginalSource == _popupRoot.Value)
        {
            // When we have capture we will get all mouse button up/down messages.

            // We should close if the press was outside.  The MouseButtonEventArgs don't tell whether we get this
            // message because we have capture or if it was legit, so we have to do a hit test.

            if (_popupRoot.Value.InputHitTest(e.GetPosition(_popupRoot.Value)) == null)
            {
                // The hit test didn't find any element; that means the click happened outside the popup.

                SetCurrentValueInternal(IsOpenProperty, BooleanBoxes.FalseBox);
            }
        }
    }
}

如果写一个 CheckBox 放在界面上,运行代码可以看到可以被打勾但是没有事件


UIElement_OnPreviewMouseDown 添加输出内容,代码如下,可以看到,没有符合预期输出

private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    Debug.WriteLine("PreviewMouseDown");
}

private void OpenPopup_OnClick(object sender, RoutedEventArgs e)
{
    Popup.PlacementTarget = (UIElement) sender;
    Popup.Placement = PlacementMode.Mouse;
    Popup.IsOpen = true;
}

Original: https://www.cnblogs.com/lindexi/p/16733246.html
Author: lindexi
Title: WPF 已知问题 Popup 吃掉 PreviewMouseDown 事件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/577732/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 系统执行命令记录到message 日志文件中

    直接 配置 /etc/profile 配置文件添加一样,简单高定。 export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; e…

    Linux 2023年6月8日
    067
  • CentOS.7下安装配置FTP和SFTP服务

    一: FTP Centos7中默认已经安装了sshd服务(sftp), vsftpd需要手动安装 1、安装并启动FTP服务 1.1 安装vsftp d 使用 yum 安装 vsft…

    Linux 2023年6月6日
    0120
  • .Net中Aspose组件调试技巧

    | 0.48分钟 | 774.4字符 | 1、引言&背景 2、水印或截断问题解决 3、即时调试的技巧 4、声明与参考资料 | SCscHero | 2022/6/4 PM2…

    Linux 2023年6月13日
    073
  • docker安装redis

    安装镜像 docker pull redis:7.0 下载配置文件 wget http://download.redis.io/redis-stable/redis.conf 修改…

    Linux 2023年6月7日
    0118
  • LeetCode-210. 课程表 II

    题目来源 题目详情 现在你总共有 numCourses 门课需要选,记为 0 到 numCourses – 1。给你一个数组 prerequisites ,其中 prerequis…

    Linux 2023年6月7日
    074
  • 微信小程序大型系统架构中应用Redis缓存要点

    在大型分布式系统架构中,必须选择适合的缓存技术以应对高并发,实现系统相应的高性能,酷客多小程序经过慎重选型,选择了采用基于腾讯云服务的Redis弹性缓存技术,结合Redis官方推荐…

    Linux 2023年5月28日
    090
  • 关于在Rocky linux下安装dotnet sdk不成功的问题

    Rocky Linux 9,运行 dnf install -y dotnet-sdk-6.0 一切正常,运行起来非常顺利,安装完毕。但是非常诡异,运行 dotnet –list-…

    Linux 2023年6月6日
    0108
  • shell中的##*,%%*问题

    假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#/}:删掉第一个 / 及其左边的字符串…

    Linux 2023年5月28日
    064
  • 实验1:SDN拓扑实践

    实验1:SDN拓扑实践 基础要求 a) mininet运行结果图 b)2的执行结果截图 2.a)3台交换机,每个交换机连接1台主机,3台交换机连接成一条线。 2.b)3台主机,每个…

    Linux 2023年6月7日
    092
  • linux配置密钥登录

    一、前言: ssh远程登录密码认证的方式有三种,password、Keyboard Interactive、Public Key前面两种方式就是密码认证,含义都是一样大同小异。第三…

    Linux 2023年6月8日
    073
  • bat-Windows的文件夹备份

    REM program:用于HTS的Windows机器&#x5…

    Linux 2023年6月7日
    092
  • Go-channel

    (1)channel本质就是一个数据结构——队列 (2)数据先进先出 (3)线程安全,多goroutine访问时,不需要加锁,channel本身就是线程安全的 (4)channel…

    Linux 2023年6月8日
    095
  • vert.x-快速入门

    vert.x是Eclipse软件基金会顶级java开源项目之一,它基于netty的、运行在jvm之上的、支持多种编程语言的高性能异步、非阻塞、响应式全栈java web框架。它在t…

    Linux 2023年6月8日
    098
  • firewalld dbus接口使用指南

    404. 抱歉,您访问的资源不存在。 可能是URL不正确,或者对应的内容已经被删除,或者处于隐私状态。 [En] It may be that the URL is incorre…

    Linux 2023年5月27日
    0107
  • 软件工程 结构化设计方法 第3篇随笔

    * 调用:模块间的一种关系,模块A为了完成其任务必须依赖其他模块 ​ ——————> Original: https://www.cnblogs.com/shuisanya/…

    Linux 2023年6月7日
    0134
  • 数据结构 单链表

    cpp;gutter:true;</p> <h1>include</h1> <h1>define null 0;</h1&gt…

    Linux 2023年6月13日
    075
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球