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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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