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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
H
Hacker News: Front Page
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
Lohrmann on Cybersecurity
Cisco Talos Blog
Cisco Talos Blog
O
OpenAI News
S
Securelist
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
H
Heimdal Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
D
Docker
D
DataBreaches.Net
A
About on SuperTechFans
T
Tor Project blog
V
V2EX
G
Google Developers Blog
博客园 - Franky
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
I
InfoQ
H
Help Net Security
V2EX - 技术
V2EX - 技术
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
SecWiki News
SecWiki News
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
小众软件
小众软件
B
Blog
T
Threatpost
P
Palo Alto Networks Blog
博客园 - 【当耐特】
L
LangChain Blog
AWS News Blog
AWS News Blog
月光博客
月光博客
宝玉的分享
宝玉的分享

博客园 - 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 属性
  • 控件卸载时会自动清理事件监听