【问题标题】:wpf edit item in listbox when a button is clicked somwhere on the form当单击表单上某处的按钮时,wpf 编辑列表框中的项目
【发布时间】:2026-01-23 14:05:02
【问题描述】:

使用this question (Inline editing TextBlock in a ListBox with DataTemplate (WPF) 我现在有一个ListBox,可以双击它来编辑其中的项目。我现在想要的是在表单上有一个Button,单击它时会将一个新项目添加到ListBox(这很容易),然后将ListBoxItem更改为编辑模式,以便用户可以输入马上就值了。如何选择正确的ListBoxItem,然后在其中找到TextBlockTextBox,并使用SelectedIndex 更改它们的可见性?

【问题讨论】:

    标签: c# wpf events button listbox


    【解决方案1】:

    我知道这是一个非常晚的答案,但是您是否考虑过在您的项目中添加 BeginEditEndEdit 方法?然后您可以执行以下操作:

    CustomListBoxItem foo = new CustomListBoxItem();
    customListBoxInstance.Add(foo);
    foo.BeginEdit();
    

    我必须使用几个需要创建并立即进入编辑模式的自定义控件来做到这一点。你最终会得到类似的东西:

    private void TextBlock1_DoubleClick(object sender, RoutedEventArgs e)
    {
        BeginEdit();
    }
    
    public void BeginEdit()
    {
        // Code to put the item into edit mode.
    }
    

    我需要查看更多代码才能给出更准确的答案,但根据我的经验,这对于从该控件范围之外控制控件是否处于编辑模式非常有效。

    【讨论】:

      最近更新 更多