WPF数据网格单元格值更改事件

28

我的设置看起来像这样:

// myDG is a DataGrid whose columns are DataGridTextColumn
ObservableCollection<MyItem> myOC;
// myOC is populated with some new MyItem
myDG.ItemsSource = myOC;

MyItem 实现了 INotifyPropertyChanged 接口。正确的方法是如何捕获用户输入单元格值的?

我尝试在 MyItem 上捕获 PropertyChanged,但我也会定期后台更新值(想法是当用户手动编辑值时,触发一个标志,告诉周期性计算避免覆盖手动输入的数据)。因此,PropertyChanged 捕获了所有东西,包括我不想要的周期性更新。我想可能可以让它工作(通过在进行周期性计算时设置标志,然后在 PropertyChanged 事件处理程序上检查缺少标志--但我想知道是否有更简单的解决方案)。

我尝试捕获 myDG.CurrentCellChanged,但每次用户更改单元格选择时都会触发,而不是特定地编辑单元格内容时触发。

编辑:这是 XAML:

<DataGrid x:Name="myDG" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Col2" Binding="{Binding Prop2}" IsReadOnly="False"/>
    </DataGrid.Columns>
</DataGrid>

这是 MyItem 的实现(使用了 Fody/PropertyChanged):

[ImplementPropertyChanged]
class MyItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public MyItem()
    {
        Prop1 = Prop2 = "";
    }
}

你能展示一下你的 XAML 实现吗? - Abin
只需要更改setter方法吗? - Nika Javakhishvili
你有看过DataGrid所公开的事件列表吗?https://msdn.microsoft.com/zh-cn/library/system.windows.controls.datagrid_events(v=vs.100).aspx - AnjumSKhan
2个回答

47

解决方案是捕获 CellEditEnding 事件。

// In initialization
myDG.CellEditEnding += myDG_CellEditEnding;

void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        var column = e.Column as DataGridBoundColumn;
        if (column != null)
        {
            var bindingPath = (column.Binding as Binding).Path.Path;
            if (bindingPath == "Col2")
            {
                int rowIndex = e.Row.GetIndex();
                var el = e.EditingElement as TextBox;
                // rowIndex has the row index
                // bindingPath has the column's binding
                // el.Text has the new, user-entered value
            }
        }
    }
}

@SUB-HDR 删除操作可以使用以下代码: if (e.Key == Key.Delete) { MyItemList.Remove((MyItem)myDG.SelectedItem); } - Heisenberg

1
你可以通过使用 CellEditEnding 事件来实现这一点,另外,在 DataGridTextColumn 中还需要添加一些属性,如下所示:
<DataGrid x:Name="myDG" CellEditEnding="myDG_CellEditEnding"  ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" 
 IsReadOnly="True"/>
 <DataGridTextColumn x:Name="dataGridTextColumn"Header="Col2" Binding="{Binding Prop2,  UpdateSourceTrigger=LostFocus, Mode=TwoWay}" Width="*" />
    </DataGrid.Columns>
</DataGrid>

在C#中

 private void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
            string prop1 = (e.Row.Item as DataRowView).Row[1].ToString();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接