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>
The
DataContext
of eachListBoxItem
is the string itself, so the path of your binding is empty (.
).TwoWay
andOneWayToSource
bindings require a path, since you can't just replace the currentDataContext
. So you need to wrap your string in an object that exposes the string as a property:Expose the strings as a list of
StringItem
:And bind to the
Value
property:Note that
StringItem
will also need to implementINotifyPropertyChanged
so that bindings are automatically updated. You should also expose the list as anObservableCollection<T>
rather than aList<T>
May be it helsp?
If I didn't misunderstand your question, it is pretty easy to implement. Look:
you can create a DataGridTemplateColumn.CellEditingTemplate with an itemscontrol and textboxes to edit your items