如何在绑定到 List<string> 时使 ListBox 可编辑? [英] How to make ListBox editable when bound to a List&lt;string&gt;?

查看:30
本文介绍了如何在绑定到 List<string> 时使 ListBox 可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:基本问题是将列表绑定到列表框(或任何其他控件).所以我正在编辑问题.

Edit: The basic problem is binding a List to ListBox(or any other control). So I am editing the question.

我将一个字符串列表绑定到一个 ListBox,如下所示.但是,当我更改文本框的内容时,它不会更改源列表中的字符串.为什么?

I bound a list of string to a ListBox as below. However when I change the contents of the textbox it is not changing the string in the source list.Why?

  public partial class MainWindow : Window
{
    List<string> _nameList = null;

    public List<string> NameList
    {
        get
        {
            if (_nameList == null)
            {
                _nameList = new List<string>();
            }
            return _nameList;
        }
        set
        {
            _nameList = value;
        }
    }
    public MainWindow()
    {
        NameList.Add("test1");
        NameList.Add("test2");
        InitializeComponent();
    }

和 XAML

 <ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding .,Mode=OneWayToSource ,  UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

推荐答案

每个ListBoxItemDataContext就是字符串本身,所以你绑定的路径是空的(<代码>.).TwoWayOneWayToSource 绑定需要一个路径,因为您不能只替换当前的 DataContext.因此,您需要将字符串包装在一个将字符串作为属性公开的对象中:

The DataContext of each ListBoxItem is the string itself, so the path of your binding is empty (.). TwoWay and OneWayToSource bindings require a path, since you can't just replace the current DataContext. So you need to wrap your string in an object that exposes the string as a property:

public class StringItem
{
    public string Value { get; set; }
}

将字符串公开为 StringItem 的列表:

Expose the strings as a list of StringItem:

public partial class MainWindow : Window
{
    List<StringItem> _nameList = null;

    public List<StringItem> NameList
    {
        get
        {
            if (_nameList == null)
            {
                _nameList = new List<StringItem>();
            }
            return _nameList;
        }
        set
        {
            _nameList = value;
        }
    }
    public MainWindow()
    {
        NameList.Add(new StringItem { Value = "test1" });
        NameList.Add(new StringItem { Value = "test2" });
        InitializeComponent();
    }

并绑定到 Value 属性:

<ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

请注意,StringItem 还需要实现 INotifyPropertyChanged 以便自动更新绑定.您还应该将列表公开为 ObservableCollection 而不是 List

Note that StringItem will also need to implement INotifyPropertyChanged so that bindings are automatically updated. You should also expose the list as an ObservableCollection<T> rather than a List<T>

这篇关于如何在绑定到 List<string> 时使 ListBox 可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆