【问题标题】:WPF DataGrid cell value changed eventWPF DataGrid 单元格值更改事件
【发布时间】:2016-03-29 03:57:40
【问题描述】:

我的设置如下所示:

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

其中MyItem 实现INotifyPropertyChanged。当 user 向单元格输入值时,正确捕获的方法是什么?

我已经尝试在MyItems 上捕获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 = "";
    }
}

【问题讨论】:

标签: c# wpf datagrid


【解决方案1】:

解决方案是捕获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); }
【解决方案2】:

您可以通过使用 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();
        }

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2016-03-19
    • 2020-03-27
    • 2015-11-19
    相关资源
    最近更新 更多