-1

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".

  • A string is immutable and cannot be changed. – mm8 Apr 30 '20 at 14:48
  • `OberservableCollection` only observes the `Collection` state, eg. `Count`, `Index of Items` for example when you call `.Add`, but it does NOT `observe` each individual item in the list. – Rand Random Apr 30 '20 at 14:53

1 Answers1

1

A string is immutable which means that it cannot be changed.

If you change the type of your source collection from ObservableCollection<string> to ObservableCollection<YourClass> where YourClass is a custom type with a public string property, you could bind to and set this one:

<TextBox Text="{Binding Path=TheProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

public class YourClass : INotifyPropertyChanged
{
    private string _theProperty;
    public string TheProperty
    {
        get { return _theProperty; }
        set { _theProperty = value; OnPropertyChanged(nameof(TheProperty); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88