Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

wpf - How to make ListBox editable when bound to a List<string>?

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

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();
    }

And the 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; }
}

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();
    }

And bind to the Value property:

<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>

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...