10

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);
}
Gilad
  • 6,437
  • 14
  • 61
  • 119
jchristof
  • 2,794
  • 7
  • 32
  • 47
  • also please have a look here http://stackoverflow.com/questions/18720892/wpf-datagrid-columns-how-to-manage-event-of-value-changing – Gilad Jan 26 '15 at 20:03
  • Unless I'm missing something, I don't think that helps - I've bound the data grid selection changed and that works fine. I now need to observe a cell's data value before and after it's edited - hopefully both pieces of data would be available in one of the callback functions. – jchristof Jan 27 '15 at 14:30
  • this worked for me, have a look at http://stackoverflow.com/questions/3938040 – toster-cx Jun 23 '15 at 12:32

2 Answers2

6

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

jchristof
  • 2,794
  • 7
  • 32
  • 47
2

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>
Gilad
  • 6,437
  • 14
  • 61
  • 119
  • I took the approach of having my row data objects inherit from IEditableObject. I handle the updated value in the EndEdit() interface method – jchristof Feb 12 '15 at 22:06