在我的系统中,我需要捕获并发送单元格编辑的旧值和新值。我已经读过你可以通过检查事件DataGridCellEditEndingEventArgs的EditingElement来做到这一点:
_dataGrid.CellEditEnding += (sender, e) => {
var editedTextbox = e.EditingElement as TextBox;
if (editedTextbox != null)
MessageBox.Show("Value after edit: " + editedTextbox.Text);
}
在我的情况下,数据是字典,因此EditingElement是ContentPresenter
var editedTextbox = e.EditingElement as ContentPresenter;
if (editedTextbox != null)
MessageBox.Show("Value after edit: " + editedTextbox.Content);
并且内容是原始内容,而不是新编辑的值。
我怎样才能让它发挥作用:
_dataGrid.SomeEvent(sender, e)->{
SendValues(e.oldCellValue, e.newCellValue);
}
答案 0 :(得分:5)
我采用了让我的行数据对象继承自IEditableObject的方法。我在EndEdit()接口方法
中处理更新的值答案 1 :(得分:2)
尝试绑定到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>