WPF DataGrid - 编辑结束后单元格的新值

时间:2021-12-03 22:16:31

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:

在我的系统中,我需要捕获并发送单元格编辑的旧值和新值。我已经读过你可以通过检查事件DataGridCellEditEndingEventArgs的EditingElement来做到这一点:

    _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

在我的例子中,数据是字典,因此EditingElement是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);
}

2 个解决方案

#1


5  

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

我采用了让我的行数据对象继承自IEditableObject的方法。我在EndEdit()接口方法中处理更新的值

#2


2  

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

尝试绑定到NotifyOnTargetUpdated - 希望这是你正在寻找的

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

#1


5  

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

我采用了让我的行数据对象继承自IEditableObject的方法。我在EndEdit()接口方法中处理更新的值

#2


2  

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

尝试绑定到NotifyOnTargetUpdated - 希望这是你正在寻找的

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