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

如何在ListBox中实现列表项重命名功能?

ListBox项重命名功能实现方案

以下分别提供WPF和WinForm两个常用平台的极简实现方式,交互完全对齐Windows文件资源管理器的重命名逻辑:

示例效果图


WinForm平台实现(最简单)

WinForm原生控件没有给ListBox提供内置重命名支持,推荐直接用ListView控件替代,只需要2行配置就能实现需求:

  1. 把ListView的View属性设置为List,显示效果和ListBox完全一致
  2. 开启LabelEdit属性:
listView1.LabelEdit = true;

开启后默认就支持:选中项后二次点击进入重命名、回车确认、Esc撤销、失焦自动确认的逻辑,无需额外代码。


WPF平台实现

不需要引入第三方控件,通过自定义ItemTemplate+简单事件处理即可实现:

1. 编写ListBox的XAML模板

<ListBox x:Name="MyListBox" ItemsSource="{Binding YourItems}" MouseLeftButtonUp="MyListBox_OnMouseLeftButtonUp">
    <ListBox.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <!-- 普通状态显示文本 -->
                <TextBlock x:Name="ItemText" Text="{Binding ItemName}" 
                           Visibility="{Binding IsEditing, Converter={StaticResource BoolToVis}, ConverterParameter=Inverse}" />
                <!-- 编辑状态显示输入框 -->
                <TextBox x:Name="EditBox" Text="{Binding ItemName, UpdateSourceTrigger=Explicit}"
                         Visibility="{Binding IsEditing, Converter={StaticResource BoolToVis}}"
                         LostFocus="EditBox_OnLostFocus" KeyDown="EditBox_OnKeyDown" Loaded="EditBox_OnLoaded"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

2. 编写后台交互逻辑

首先给你的列表项绑定的模型类添加IsEditing布尔属性,用来切换编辑状态:

// 列表项模型示例
public class ListItemModel : INotifyPropertyChanged
{
    private string _itemName;
    public string ItemName
    {
        get => _itemName;
        set { _itemName = value; OnPropertyChanged(); }
    }

    private bool _isEditing;
    public bool IsEditing
    {
        get => _isEditing;
        set { _isEditing = value; OnPropertyChanged(); }
    }

    // 省略INotifyPropertyChanged实现代码
}

然后添加对应的事件处理:

private DateTime _lastClickTime = DateTime.MinValue;
private void MyListBox_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // 两次点击间隔超过系统双击时间,才触发重命名,避免和双击操作冲突
    if ((DateTime.Now - _lastClickTime).TotalMilliseconds > SystemInformation.DoubleClickTime)
    {
        if (MyListBox.SelectedItem is ListItemModel item)
        {
            // 先把其他项的编辑状态关掉
            foreach (ListItemModel i in MyListBox.Items) i.IsEditing = false;
            item.IsEditing = true;
        }
    }
    _lastClickTime = DateTime.Now;
}

// 输入框加载时自动全选文本
private void EditBox_OnLoaded(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    textBox?.SelectAll();
    textBox?.Focus();
}

// 键盘事件处理
private void EditBox_OnKeyDown(object sender, KeyEventArgs e)
{
    var textBox = sender as TextBox;
    var item = textBox?.DataContext as ListItemModel;
    if (item == null) return;

    if (e.Key == Key.Enter)
    {
        // 回车确认,保存修改
        BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty)?.UpdateSource();
        item.IsEditing = false;
        e.Handled = true;
    }
    else if (e.Key == Key.Escape)
    {
        // Esc撤销,不保存
        BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty)?.UpdateTarget();
        item.IsEditing = false;
        e.Handled = true;
    }
}

// 输入框失焦自动保存
private void EditBox_OnLostFocus(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    var item = textBox?.DataContext as ListItemModel;
    if (item == null || !item.IsEditing) return;
    BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty)?.UpdateSource();
    item.IsEditing = false;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 21:15:04