S
S
SMARTi2015-05-03 08:43:49
.NET
SMARTi, 2015-05-03 08:43:49

How to get changed object when editing row in DataGrid WPF?

I change the line in the DataGrid, then I transfer the focus to another line, the RowEditEnding event is called in the DataGrid, but the old object comes in the parameters without changes.
Binding data to DataGrid:

var myObjectList = new List<MyObject>();
/* Заполняю список */
myObjectDataGrid.ItemsSource = myObjectList;

Handling the RowEditEnding event:
private void MyObjectDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
      var myObjectRow = e.Row.Item as MyObject; //Сюда приходит объект без изменений
}

Where all the same to find the changed line?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SMARTi, 2015-05-03
@SMARTi

The answer is found, in the XAML markup in the binding, you need to add the UpdateSourceTrigger=PropertyChanged property i.e. I had this:
and you need to do it like this:

<DataGrid x:Name="HistoryDataGrid" RowEditEnding="HistoryDataGrid_RowEditEnding" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Action, UpdateSourceTrigger=PropertyChanged}" Header="Действие"/>
                            <DataGridTextColumn Binding="{Binding Person, UpdateSourceTrigger=PropertyChanged}" Header="Персона"/>
                            <DataGridTextColumn Binding="{Binding Date, UpdateSourceTrigger=PropertyChanged}" Header="Дата"/>
                        </DataGrid.Columns>
                    </DataGrid>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question