You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF C#:仅在新增项时设置ItemsControl最后一项焦点

解决WPF新增ItemsControl项时TextBox自动获焦且不影响滚动位置的问题

针对你遇到的问题,推荐两种可行方案,避免全局使用FocusManager导致的滚动位置异常:

方案一:数据模型标记+Loaded事件触发焦点(MVVM友好)

  1. 修改数据模型,添加IsNew属性用于标记新增项:
public class ItemViewModel : INotifyPropertyChanged
{
    private bool _isNew;
    public bool IsNew
    {
        get => _isNew;
        set
        {
            _isNew = value;
            OnPropertyChanged();
        }
    }

    // 你的其他业务属性

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 添加新项时设置标记:
    在主ViewModel的添加方法中,给新项的IsNew赋值为true:
public ObservableCollection<ItemViewModel> Items { get; } = new();

public void AddNewItem()
{
    var newItem = new ItemViewModel { IsNew = true };
    Items.Add(newItem);
}
  1. TextBox绑定Loaded事件处理焦点:
    在XAML中给TextBox添加Loaded事件,仅当IsNew为true时获取焦点,随后重置标记:
<TextBox x:Name="ItemTextBox" Loaded="ItemTextBox_Loaded" Style="..." />

后台代码实现事件逻辑:

private void ItemTextBox_Loaded(object sender, RoutedEventArgs e)
{
    if (sender is TextBox textBox && textBox.DataContext is ItemViewModel itemVm)
    {
        if (itemVm.IsNew)
        {
            textBox.Focus();
            itemVm.IsNew = false; // 重置标记,避免后续页面加载时重复触发
        }
    }
}

方案二:添加项后直接定位UI元素(无需修改模型)

如果不想修改数据模型,可以在添加新项后,通过UI容器找到对应的TextBox并设置焦点:

  1. 在添加方法中延迟操作UI:
    利用Dispatcher等待UI更新完成后,查找目标元素:
public void AddNewItem()
{
    var newItem = new ItemViewModel();
    Items.Add(newItem);

    // 等待UI渲染完成后执行
    Application.Current.Dispatcher.BeginInvoke(() =>
    {
        // 获取ItemsControl实例(可通过Name绑定或其他方式引用)
        var container = YourItemsControl.ItemContainerGenerator.ContainerFromItem(newItem) as FrameworkElement;
        if (container != null)
        {
            var textBox = container.FindName("ItemTextBox") as TextBox;
            textBox?.Focus();
            // 可选:自动滚动到新增项位置
            YourItemsControl.ScrollIntoView(newItem);
        }
    }, DispatcherPriority.ContextIdle);
}

两种方案都能实现仅在新增项时让TextBox获焦,同时避免页面重新打开时FocusManager干扰滚动位置的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 06:55:18