惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
B
Blog
Cisco Talos Blog
Cisco Talos Blog
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
V
Visual Studio Blog
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
S
Security @ Cisco Blogs
博客园 - 聂微东
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
量子位
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tenable Blog
月光博客
月光博客
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
D
Docker
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Help Net Security
Help Net Security
D
DataBreaches.Net

博客园 - 01星河

AI时代:速度的错觉与秩序的退潮 UI 自动化测试产品深度对比分析报告 Web 自动化测试系统深度分析报告 第 7 章 系统效果评测与监控 Linux TOP 命令详解 mysql-canal-rabbitmq 安装部署教程 第 6 章 用户画像系统 Elasticsearch 模块 - Shard Allocation 机制 Kibana 插件环境搭建教程 追溯 MySQL Statement Cancellation Timer 第 5 章 机器学习技术的应用(下) 第 5 章 机器学习技术的应用(中) 第 5 章 机器学习技术的应用(上) 第 4 章 算法融合与数据血统 第 3 章 基础推荐算法 第 2 章 推荐系统的核心技术概述 阅读笔记 - 从零开始构建企业级推荐系统 第 1 章 推荐系统的时代背景 三个实例演示 Java Thread Dump 日志分析
TextBoxPopupBehavior控件
01星河 · 2025-07-19 · via 博客园 - 01星河

功能说明

一个用于 WPF TextBox 的附加行为,实现 TextBox 与 Popup 控件的联动效果:

  1. 自动弹出/关闭

    • TextBox 获得焦点时自动打开关联的 Popup
    • TextBox 失去焦点时自动关闭关联的 Popup
  2. 点击外部关闭

    • 点击 TextBox 和 Popup 外部区域时关闭 Popup
  3. 焦点状态处理

    • 解决 TextBox 保持焦点但 Popup 关闭后的重新触发问题

核心代码解析

        // 解决在 TextBox 外其他地方点击时,仅关闭 Popup,但是 TextBox 还是 Focused 状态,导致再点击进来时不会触发弹出 Popup
        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            if (AssociatedObject?.IsFocused == true && Popup != null && !Popup.IsOpen) {
                Popup.IsOpen = true;
            }
        }

        private void Window_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            if (Popup == null || !Popup.IsOpen || AssociatedObject == null || _parentWindow == null) { return; }

            Point position = e.GetPosition(null);
            Rect textBoxRect = new Rect(AssociatedObject.TranslatePoint(new Point(0, 0), _parentWindow), AssociatedObject.RenderSize);
            Rect popupRect = new Rect(Popup.TranslatePoint(new Point(0, 0), _parentWindow), Popup.RenderSize);
            if (!textBoxRect.Contains(position) && !popupRect.Contains(position)) {
                Popup.SetCurrentValue(Popup.IsOpenProperty, false);
            }
        }

使用方法

<TextBox x:Name="SearchBox"
         Width="400"
         Height="30"
         Text="{Binding ElementName=list, Path=SelectedItem.Content}">
    <hc:Interaction.Behaviors>
        <controls:TextBoxPopupBehavior Popup="{Binding ElementName=SuggestionsPopup}" />
    </hc:Interaction.Behaviors>
</TextBox>

<Popup x:Name="SuggestionsPopup"
       Placement="Bottom"
       PlacementTarget="{Binding ElementName=SearchBox}"
       StaysOpen="True">
    <Border Background="White"
            BorderBrush="Gray"
            BorderThickness="1">
        <ListBox x:Name="list"
                 Width="200"
                 Height="150">
            <ListBoxItem>选项1</ListBoxItem>
            <ListBoxItem>选项2</ListBoxItem>
            <ListBoxItem>选项3</ListBoxItem>
        </ListBox>
    </Border>
</Popup>

注意事项

  • 确保 Popup 的 PlacementTarget 正确绑定
  • 在 MVVM 模式下可通过绑定设置 Popup 属性
  • 控件卸载时会自动清理事件监听