You need to enable JavaScript to run this app. WPF如何在ListView子控件TextBox获Tab焦点时设置对应选中项
优惠活动
大模型
产品
解决方案
定价
更多

WPF如何在ListView子控件TextBox获Tab焦点时设置对应选中项

纯XAML无页面后台实现方案

方案1:使用官方XAML行为(推荐,实现最简单)

  1. 先安装NuGet包:Microsoft.Xaml.Behaviors.Wpf
  2. 在XAML头部添加命名空间引用:
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  1. 修改GridViewColumn里的TextBox代码,添加事件触发行为即可:
<TextBox Text="{Binding Value}" IsEnabled="{Binding AllowValueChanging}" MinWidth="300">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="GotFocus">
            <i:ChangePropertyAction 
                TargetObject="{Binding RelativeSource={RelativeSource AncestorType=ListView}}"
                PropertyName="SelectedItem"
                Value="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

该方案完全不需要编写任何页面后台代码,所有逻辑都在XAML中完成,无论是Tab导航还是鼠标点击聚焦到TextBox,都会自动选中当前行。


方案2:自定义附加属性(不需要额外NuGet,一次编写多处复用)

如果不想引入第三方NuGet,可以自己编写通用附加属性类,全局复用:

  1. 新建ListViewHelper.cs类:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

public static class ListViewHelper
{
    public static readonly DependencyProperty AutoSelectRowOnFocusProperty = DependencyProperty.RegisterAttached(
        "AutoSelectRowOnFocus", typeof(bool), typeof(ListViewHelper), new PropertyMetadata(false, OnAutoSelectRowOnFocusChanged));

    public static void SetAutoSelectRowOnFocus(DependencyObject element, bool value)
    {
        element.SetValue(AutoSelectRowOnFocusProperty, value);
    }

    public static bool GetAutoSelectRowOnFocus(DependencyObject element)
    {
        return (bool)element.GetValue(AutoSelectRowOnFocusProperty);
    }

    private static void OnAutoSelectRowOnFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not TextBox textBox) return;
        if ((bool)e.NewValue)
        {
            textBox.GotFocus += TextBox_GotFocus;
        }
        else
        {
            textBox.GotFocus -= TextBox_GotFocus;
        }
    }

    private static void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        if (sender is not TextBox textBox) return;
        var listView = FindAncestor<ListView>(textBox);
        if (listView == null) return;
        listView.SelectedItem = textBox.DataContext;
    }

    private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
    {
        while (current != null)
        {
            if (current is T ancestor)
            {
                return ancestor;
            }
            current = VisualTreeHelper.GetParent(current);
        }
        return null;
    }
}
  1. 在XAML中直接给TextBox添加附加属性即可,不需要其他额外代码:
<TextBox Text="{Binding Value}" IsEnabled="{Binding AllowValueChanging}" MinWidth="300"
         local:ListViewHelper.AutoSelectRowOnFocus="True"/>

内容的提问来源于stack exchange,提问作者MUT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 16:06:00