A Issue related to DataBinding with TextBlock

74 Views Asked by At

I have a class where have some get;set; properties. And OnPropertyChange event, have calculated additions of all those get;set; properties. There are other properties that just have get; and it returns a calcuation simple like:

public Double D_F_Percent { 
    get {
        return d_f / total;
    }           
}

These properties are bound to TextBlocks. Initally, the values show up as NaN. But later when I enter values for d_f, and it gets added up to toal in OnPropertyChanged.

Now my point is after calculating total how do I call this property and fire it so it gets refreshed in the TextBlock?

1

There are 1 best solutions below

2
On BEST ANSWER

You can fire the PropertyChanged anytime and the binding engine will update the UI.

It's not required to do it in a property setter. So in the method where you calculate total just raise the event with your calculated property name D_F_Percent.

See in the sample CalculateTotal method:

public class ViewModel : INotifyPropertyChanged
{
    private double d_f;
    public double D_F
    {
        get { return d_f; }
        set { d_f = value; FirePropertyChanged("D_F"); }
    }

    private double total;
    public double Total
    {
        get { return total; }
        set { total = value; FirePropertyChanged("Total"); }
    }

    public Double D_F_Percent
    {
        get { return d_f / total; }
    }

    public void CalculateTotal()
    {
        //Do some calculation set total
        FirePropertyChanged("D_F_Percent");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}