C# 获取DataGrid的OnTargetUpdated事件中的单元格id或单元格内容

C# 获取DataGrid的OnTargetUpdated事件中的单元格id或单元格内容,c#,wpf,events,datagrid,C#,Wpf,Events,Datagrid,我有一个绑定到数据的DataGrid,并且实现了OnTargetUpdated。读取/写入两个单元格变量和复选框isLive。如果我更改变量或复选框,我会跳转到OnTargetUpdated: <DataGrid AutoGenerateColumns="False" Grid.Row="3" Height="126" HorizontalAlignment="Left" Margin="-1,0,0,0" Name="dg_queue" VerticalAlignment="Top" W

我有一个绑定到数据的DataGrid,并且实现了
OnTargetUpdated
。读取/写入两个单元格
变量
和复选框
isLive
。如果我更改变量或复选框,我会跳转到
OnTargetUpdated

<DataGrid AutoGenerateColumns="False" Grid.Row="3" Height="126" HorizontalAlignment="Left" Margin="-1,0,0,0" Name="dg_queue" VerticalAlignment="Top" Width="1446" Grid.ColumnSpan="6" ItemsSource="{Binding QueueItems}" TargetUpdated="OnTargetUpdated">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Width="30" Binding="{Binding Id, StringFormat={}{0:N0}}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Submit Time" Width="80" Binding="{Binding Submit_Time, Converter={StaticResource TimeConverter}}" />
        <DataGridTextColumn Header="Strategy" Width="80" Binding="{Binding Strategy}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Variables" Width="200" Binding="{Binding Variables, NotifyOnTargetUpdated=True}" IsReadOnly="False"/>
        <DataGridCheckBoxColumn Header="Is Live" Width="SizeToHeader" Binding="{Binding Is_Live, NotifyOnTargetUpdated=True}" IsReadOnly="False"/>                
        <DataGridTextColumn Header="Status" Width="60" Binding="{Binding Status}" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

我的问题是,如何从我的发件人或我的参数中判断我更改了什么(即复选框或文本框(变量)或我不关心的东西)来触发事件?

我认为对于您的任务,更适合事件:

在提交或取消单元格编辑之前发生

使用的示例:

XAML

<DataGrid Name="MyDataGrid" 
          AutoGenerateColumns="False" 
          CellEditEnding="MyDataGrid_CellEditEnding" ... />           
private void MyDataGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;

    if (e.EditAction == DataGridEditAction.Commit)
    {
        if (e.Column.Header.Equals("Variables"))
        {
            TextBox textBox = e.EditingElement as TextBox;
            MessageBox.Show(textBox.Text);
        }
        else if (e.Column.Header.Equals("IsLive"))
        {
            CheckBox checkBox = e.EditingElement as CheckBox;
            MessageBox.Show(checkBox.IsChecked.ToString());
        }
    }
}
虽然它可以工作,但我认为它看起来很难,因为它是WinForms的一种风格,而不是WPF。在这种情况下,您可以跟踪事件
INotifyPropertyChanged
接口,并执行以下操作:

摘自答案:

在视图模型构造函数中:

SelectedItem.PropertyChanged += SelectedItem_PropertyChanged;
在视图模型中:

private void SelectedItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    // this will be called when any property value
    // of the SelectedItem object changes
    if (e.PropertyName == "YourPropertyName") DoSomethingHere();
    else if (e.PropertyName == "OtherPropertyName") DoSomethingElse();
}
在UI中:

<DataGrid ItemsSource="{Binding Items}"
          SelectedItem="{Binding SelectedItem}" ... />

此外,我建议您查看引用的答案:

<DataGrid ItemsSource="{Binding Items}"
          SelectedItem="{Binding SelectedItem}" ... />