3

Possible Duplicate:
How to make ListBox editable when bound to a List<string>?

I'm trying to set a two-binding between a List named "ListStr" object and a ListBox WPF control. Besides I want the items to be editable, so I added a DataTemplate with TextBoxes expecting that it would modify the ListStr items straight away via TextBoxes.

But when I'm attempting to edit one of them, it doesn't work...

Any Idea ?

PS: I've tried to add the Mode=TwoWay parameter, but it's still not working

Here is the XAML :

<ListBox ItemsSource="{Binding Path=ListStr}" Style="{DynamicResource ResourceKey=stlItemTextContentListBoxEdit}" />

Here is the style code :

<Style x:Key="stlItemTextContentListBoxEdit" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="#FF0F2592" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Height" Value="150" />
<Setter Property="Width" Value="200" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="ItemTemplate" Value="{DynamicResource ResourceKey=dtplItemTextContentListBoxEdit}" /></Style>

And the DataTemplate:

<DataTemplate x:Key="dtplItemTextContentListBoxEdit">
    <TextBox Text="{Binding Path=.}" Width="175" />
</DataTemplate>
Community
  • 1
  • 1
Pamplemousse
  • 113
  • 1
  • 3
  • 9

2 Answers2

8

Two way binding does not work when you use {Binding Path=.} (which is long for {Binding}). Keep in mind what is happening.

The ListBox is given a list of objects, which it then creates one ListBoxItem for each item. The DataContext of the ListBoxItem is then set to that object. When you use {Binding}, you are saying to just use the object in the DataContext. When you type in the TextBox, what would it update? It can't set the DataContext and it has no idea where the object came from (so it can't update your list/array).

Where two way binding does work, is when you bind to a property on that object. But not when you bind to the object itself.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • I'm not sure so: What is the DataContext of each ListBoxItem ? – Pamplemousse Dec 19 '12 at 14:15
  • So my problem is really simple: I want to modify a List which is bound with a ListBox. How can I do that ? – Pamplemousse Dec 19 '12 at 14:23
  • 1
    you need an item with a setter so the binding will change your data , create a class with property and use it insted of the string and bind to the property – ZSH Dec 19 '12 at 15:04
  • @Pamplemousse - You won't be able to modify a List using a ListBox like that. See ZSH's answer for how it could be done. – CodeNaked Dec 19 '12 at 15:43
3
    public class ViewModel
{
    ObservableCollection<TextItem> _listStr = new ObservableCollection<TextItem> { new TextItem("a"), new TextItem("b"), new TextItem("c") };

    public ObservableCollection<TextItem> ListStr
    {
        get  { return _listStr; }  // Add observable and propertyChanged here also if needed
    }
}

public class TextItem : NotificationObject // (NotificationObject from Prism you can just implement INotifyPropertyChanged)
{
    public TextItem(string text)
    {
        Text = text;
    }

    private string _text;
    public string Text
    {
        get { return _text; }
        set
        {
            if (_text != value)
            {
                _text = value;
                RaisePropertyChanged(() => Text);
            }
        }
    }
}


xaml:

 <DataTemplate>
    <TextBox Text="{Binding Text}" Width="175" />
 </DataTemplate>
ZSH
  • 905
  • 5
  • 15