Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid - cell's new value after edit ending

In my system I need to capture and send the old and new value of a cell edit. I've read that you can do this by inspecting the EditingElement of the event DataGridCellEditEndingEventArgs like this:

    _dataGrid.CellEditEnding += (sender, e) => {
      var editedTextbox = e.EditingElement as TextBox;

      if (editedTextbox != null)
      MessageBox.Show("Value after edit: " + editedTextbox.Text);
}

In my case, the data is a dictionary so the EditingElement is a ContentPresenter

var editedTextbox = e.EditingElement as ContentPresenter;
if (editedTextbox != null)
  MessageBox.Show("Value after edit: " + editedTextbox.Content);

and the Content is the original, not the new edited value.

How can I get this to work:

_dataGrid.SomeEvent(sender, e)->{
  SendValues(e.oldCellValue, e.newCellValue);
}
like image 958
jchristof Avatar asked Jan 26 '15 19:01

jchristof


2 Answers

I took the approach of having my row data objects inherit from IEditableObject. I handle the updated value in the EndEdit() interface method

like image 194
jchristof Avatar answered Oct 29 '22 14:10

jchristof


Try to bind into NotifyOnTargetUpdated - hope this is what you are looking for

<DataGrid Name="datagrid" AutoGenerateColumns="False" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
    <DataGrid.Columns>
        <DataGridTextColumn  Header="Title" Binding="{Binding Path=Name,NotifyOnTargetUpdated=True}" Width="300">
            <DataGridTextColumn.EditingElementStyle>
                <Style TargetType="{x:Type TextBox}">
                    <EventSetter Event="LostFocus" Handler="Qty_LostFocus" />
                    <EventSetter Event="TextChanged" Handler="TextBox_TextChanged" />
                    <EventSetter Event="Binding.TargetUpdated" Handler="DataGridTextColumn_TargetUpdated"></EventSetter>
                </Style>
            </DataGridTextColumn.EditingElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
like image 41
Gilad Avatar answered Oct 29 '22 13:10

Gilad