I searched a solution for roughly 3 days, but i can't get it done.
I have a Observable Collection of Strings (which get its value out of a db, this works) in a class. These are bound to a DataGrid, which creates a TextBox for every element containing the Binding as Text.
My Code behind can change those strings in the observable collection, i can delete single rows, i can add rows. Everything is showing correctly and is updated in the view.
But somehow, if i change the text in the TextBox while runtime, the update isn't saved in the observable collection. It always stays the same.
Here is what I have:
XAML
<DataGrid ItemsSource="{Binding Path=DeviceType}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
MyClass
class Test: INotifyPropertyChanged
{
private ObservableCollection<string> _deviceType = new ObservableCollection<string>();
public ObservableCollection<string> DeviceType
{
get
{
return _deviceType;
}
set
{
_deviceType = value;
OnPropertyChanged("DeviceType");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
So, what do I have to do that a change of the Text in the TextBox also updates the ObservableCollection Item? I found a post that suggested the use of BindingList instead of ObservableCollection, but it doesn't work either. Binding a String to a TextBox works of course, but why doesn't it work with ObservableCollection?
For Example:
If my Observable Collection consists of 3 Elements ("John", "Doe", "Mary"), the DataGrid shows each one in a row. If i write Josh instead of John in that Box, the first element of my Collection still contains "John".