简体   繁体   English

区分用户或程序是否更改了 wpf 数据网格中的值

[英]Distinguish if the user or the program changed a value in a wpf datagrid

i have a mvvm application in which i have a DataGrid in Wpf and want to get notified if a user changes a value in a column.我有一个 mvvm 应用程序,其中我在 Wpf 中有一个 DataGrid,并且希望在用户更改列中的值时得到通知。

All dataGridColumns have a binding to my viewmodel, which invokes a PropertyChanged Command if it gets changed.所有 dataGridColumns 都绑定到我的视图模型,如果它发生更改,它会调用 PropertyChanged 命令。 Now the Problem is, how i can determine if the property has been changed by the user or by the code?现在的问题是,我如何确定该属性是否已被用户或代码更改? Because i only want to add a note to the corresponding line when it has been changed manually by the user.因为我只想在用户手动更改后向相应行添加注释。

The Column of interest is implemented like this in wpf:感兴趣的列在wpf中是这样实现的:

 <DataGridTextColumn
  Header="DIL"
  Binding="{Binding DilutionFactor, StringFormat={}{0:n4}}" 
  Visibility="{Binding Value,
  Source={StaticResource DilVis}, 
  Converter={StaticResource BooleanToVisibilityConverter}}" 
  IsReadOnly="False"/>

Which is bound to the ViewModel Property:它绑定到 ViewModel 属性:

    public double DilutionFactor
    {
        get { return _dilutionFactor; }
        set
        {
            _dilutionFactor = value;
            Update(); // PropertyChanged Command
            UpdatePipetteVolumes(); // function to update corresponding volumes
        }
    }

Is there a event or anything i can use to trigger a method when the user changes the value of the DIL column, which is not triggered when the code updates the value?当用户更改 DIL 列的值时,是否有事件或任何我可以用来触发方法的方法,而当代码更新值时不会触发该方法?

You could set a boolean flag each time before you programmatically change the value.您可以在每次以编程方式更改值之前设置 boolean 标志。 Then in the property setter you can check that property to see if the user invoked the change.然后在属性设置器中,您可以检查该属性以查看用户是否调用了更改。 However, this method might need a lot of code changes for heavily used properties.但是,此方法可能需要对大量使用的属性进行大量代码更改。

Another way: Add a second property which just sets and returns the existing property.另一种方法:添加第二个属性,它只是设置并返回现有属性。 Then use that new property for the datagrid binding:然后将该新属性用于数据网格绑定:

public double DilutionFactorUser
{
    get { return this.DilutionFactor; }
    set
    {
        this.DilutionFactor = value;
        // Here comes the code that is only executed on user-invoked changes
    }
}

public double DilutionFactor
{
    get { return _dilutionFactor; }
    set
    {
        _dilutionFactor = value;
        Update(); // PropertyChanged Command
        UpdatePipetteVolumes(); // function to update corresponding volumes
    }
}

Set up your Datagrid to bind to DilutionFactorUser设置您的 Datagrid 以绑定到DilutionFactorUser

You could also use the DataGrid.CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) event instead of looking the Property.您还可以使用 DataGrid.CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 事件而不是查看属性。 e.Row.Item will have the data you are binding to. e.Row.Item 将包含您要绑定的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM