Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mvvm

wpf

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.

All dataGridColumns have a binding to my viewmodel, which invokes a PropertyChanged Command if it gets changed. 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:

 <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:

    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?

like image 237
try_3xcept Avatar asked Nov 07 '25 08:11

try_3xcept


1 Answers

You could set a boolean flag each time before you programmatically change the value. 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

like image 114
NoConnection Avatar answered Nov 10 '25 01:11

NoConnection



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!