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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28156929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:52:48  来源:igfitidea点击:

WPF DataGrid - cell's new value after edit ending

c#.netwpfdatagrid

提问by jchristof

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);
}

采纳答案by jchristof

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() 接口方法中处理更新的值

回答by Gilad

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>