0

I have a table that stores data received from an external Internet resource. When changing a table cell, I need to send new data to the server. The question is how to correctly bind the property that will store the new data?

The View DataGrid XAML:

<DataGrid Grid.Row="2"
                  ItemsSource="{Binding Properties, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  AutoGenerateColumns="False"
                  IsReadOnly="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=name}"
                                     IsReadOnly="False"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Value">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=value}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

And View Model Property of collection that places in DataGrid:

private ObservableCollection<IProperties> _Properties;
public ObservableCollection<IProperties> Properties
    {
        get => _Properties;
        set
        {
            _Properties = value;
            OnPropertyChanged(nameof(Properties));
        }
    }

1 Answers1

0

There are a few possible ways to do that:

  1. In your concrete implementation of the IProperty interface, put your code in the setter of your value property.

  2. Alternatively, you can make your IProperty implementation also implement INotifyPropertyChanged and subscribe to the PropertyChanged event of all items in your ObservableCollection. Take care to attach and de-attach event handlers as you add or remove items from the collection.

  3. Instead of doing the steps in option 2 manually, you can use a special kind of ObservableCollection for that purpose. Or use a BindingList instead, which already covers this use case and raises the ListChanged event whenever one of its items raises its PropertyChanged event.

Heinzi
  • 167,459
  • 57
  • 363
  • 519