I'm trying to do something very simple : I want to display an ObservableCollection in an ItemsControl. I also want the user to be able to edit the strings in the ObservableCollection.
My viewmodel is pretty straightforward :
public class MainViewModel : ViewModelBase
{
public ObservableCollection<string> Values { get; set; }
public MainViewModel()
{
Values = new ObservableCollection<string> { "foo", "bar" };
}
}
In my view, I define a DataTemplate for my ItemsControl to display a textbox inside each item, no problem here :
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBox Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Everything seems to work smoothly, I can see the content of my ObservableCollection in my view. However, when I edit the text in the textbox, my ObservableCollection is not updated, the values are still the same.
The only workaround I've found to make this work is to create a class :
public class StringWrapper
{
public string Value { get; set; }
}
Then I change the type of my ObservableCollection to ObservableCollection<StringWrapper>
, and in my DataTemplate, I bind the Text property of my TextBox to Value. It works, but it's ugly.
So my question is, is there a way to bind an ObservableCollection to an ItemsControl and be able to update the values ?