Is it guaranteed that on IValueConverter ConvertBa

2019-09-17 02:32发布

问题:

Is it guaranteed that on IValueConverter ConvertBack call other views would update and how to enforce it?

I have 4 parameters in a List. Each pair of them can be calculated from another two in a manner like:

A = f(a,b)
B = f2(a,b)
a = f3(A,B)
b = f4(A,B)

I have them all rendered in one in a ItemsControl. I created a IValueConverter that can Convert and ConvertBack on demand. During Conversion Back of one item values for other three are updated in the source List. I wonder if it is guaranteed that after one List Item ConvertBack call others would be invoked?

回答1:

No, this is not guaranteed. IValueConverter does only convert the value which should be set on the model.

For the view to update based on that change, your model should implement INotifyPropertyChanged and raise the PropertyChanged event after a property was set.

Here is an example of a class that implements INotifyPropertyChanged:

public class TestClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string property;
    public string Property 
    { 
        get { return property; }
        set
        {
            property = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Property)));
        }
    }
}

Please note that you'd normally use a base class that implements that interface (provided by all MVVM libraries and frameworks).