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

时间:2014-02-08 02:57:09

标签: c# wpf events datagrid

我有一个DataGrid绑定到数据,我实现了OnTargetUpdated。两个单元格variables和复选框isLive是可读/写的。如果我更改变量或CheckBox,我会跳转到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>

如果我更改变量或CheckBox,我会按预期跳转到OnTargetUpdated

private void OnTargetUpdated(Object sender, DataTransferEventArgs args)
{
    // Something Changed in the Grid.  
    // if is Is_Live or Variables let's do something useful            
}

我的问题是如何告诉我的发件人或我的args我更改了什么(即CheckBox或TextBox(变量)或我不关心的事情)来触发事件?

1 个答案:

答案 0 :(得分:0)

我认为您的任务更适合CellEditEnding事件:

  

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

使用示例:

<强> XAML

<DataGrid Name="MyDataGrid" 
          AutoGenerateColumns="False" 
          CellEditEnding="MyDataGrid_CellEditEnding" ... />           

<强> Code-behind

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界面,并执行以下操作:

  

取自答案:WPF DataGrid columns: how to manage event of value changing

在视图模型构造函数中:

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

在用户界面中:

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

另外,我建议看一下引用的答案:

WPF DataGrid columns: how to manage event of value changing

相关问题