3

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>
Jimmy
  • 3,224
  • 5
  • 29
  • 47

4 Answers4

15

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>

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

May be it helsp?

<ListBox Name="lsbList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=Value}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
IVerzin
  • 868
  • 7
  • 9
  • You can edit the UI like this, but the edit wont reflect in the list object – Jimmy Apr 27 '11 at 12:37
  • What do you mean? NameColumnField - list of your objects. For example, one of its properties has name Value. The Value is bound to TextBox in DataTemplate. And if you change it in textbox, it will not be changed in bounded object? – IVerzin Apr 27 '11 at 14:04
  • Also, please check the edited question where I come down to the actual issue. – Jimmy Apr 27 '11 at 15:27
  • +1, After reading Thomas's answer I understand what you are trying to say.+ – Jimmy Apr 27 '11 at 16:18
0

If I didn't misunderstand your question, it is pretty easy to implement. Look:

<ComboBox Text="My Comment 5 with addition." IsEditable="True" Height="25" Width="200">
        <ComboBoxItem>My comment1</ComboBoxItem>
        <ComboBoxItem>My comment2</ComboBoxItem>
</ComboBox>
Cary
  • 372
  • 1
  • 5
  • 14
0

you can create a DataGridTemplateColumn.CellEditingTemplate with an itemscontrol and textboxes to edit your items

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • You can edit the UI like this, but the edit wont reflect in the list object – Jimmy Apr 27 '11 at 12:38
  • Please check the edited question. Even if I can edit the textbox in the UI it wont reflect in the source – Jimmy Apr 27 '11 at 15:33