WPF DataGrid 单元格值更改事件 [英] WPF DataGrid cell value changed event

查看:62
本文介绍了WPF DataGrid 单元格值更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置如下所示:

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

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

where MyItem implements INotifyPropertyChanged. What's the way to properly catch when the user inputs a value into a cell?

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

I've tried catching PropertyChanged on the MyItems, but I also update the values periodically in the background (the idea is that when the user manually edits the value, a flag is triggered that tells the periodic calculation to avoid overwriting the manually entered data). So PropertyChanged catches everything, including the periodic updates, which I don't want. I suppose it's possible to make this work (by setting a flag when I do the periodic calculation, then checking for absence of flag on the PropertyChanged event handler -- but I want to know if there's a simpler solution.)

我试过捕捉 myDG.CurrentCellChanged 但每次用户更改单元格选择时都会触发,而不是在他们编辑单元格内容时触发.

I've tried catching myDG.CurrentCellChanged but that's triggered every time the user changes the cell selection, not specifically when they edit cell contents.

这是 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):

Here is the MyItem implementation (uses Fody/PropertyChanged):

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

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

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

推荐答案

解决方案是捕获 CellEditEnding 事件.

The solution was to catch the CellEditEnding event.

// 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
            }
        }
    }
}

这篇关于WPF DataGrid 单元格值更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆