1

I have 3 textbox's in WPF bound to three properties of my business object. The texboxes allow the user to enter three different lengths.

I then have a number of textblocks bound to other properties which display simple calculations from the 3 inputs. The values of these properties are calculated in the get method of each property as follows:

public double Length1 { get; set }
public double Length2 { get; set }
public double Length3 { get; set }
public double Result1 
{
    get { return Length1 - Length2 - Length3; }
}
public double Result2
{
    get { return Length1 + Length2 + Length3; }
}

How can i update the bindings of Result1 and Result2 after changes made to Lengths1,2 or 3?

Thanks

Cadair Idris
  • 557
  • 1
  • 7
  • 20

2 Answers2

3

If your ViewModel is imnplementing the INotifyPropertyChanged event, you want to (unfortunately) ditch those auto properties, and raise the PropertyChanged event whenever Length1, Length2 or Length3 are changed.

PropertyChanged(this, new PropertyChangedEventArgs("Result1"));

private double _length1;
public double Length1 {
   get { return _length1; }
   set {
       _length = value;  
       PropertyChanged(this, new PropertyChangedEventArgs("Result1"));
       PropertyChanged(this, new PropertyChangedEventArgs("Result2"));
       PropertyChanged(this, new PropertyChangedEventArgs("Length1"));
   }

But this gets tiring pretty quick, so I usually implement a helper method:

void RaiseThese(params string[] properties){
   foreach(string prop in properties)
       PropertyChanged(this, new PropertyChangedEventArgs(prop));
}

And then

public double Length1 {
   get { return _length1; }
   set {
       _length = value;  
       RaiseThese("Result1", "Result2", "Length1");
   }
Adam Rackis
  • 79,454
  • 49
  • 255
  • 377
2

Use INotifyPropertyChanged and raise the Event for Result1 and Result2 in Length1, Length2 and Length3.

private double _length1;
public double Length1
{
    get { return _length1; }
    set
    {
        if (_length1 == value) return;
        _length1 = value;
        OnPropertyChanged("Length1");
        OnPropertyChanged("Result1");
        OnPropertyChanged("Result2");
    }
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}   
LPL
  • 16,007
  • 6
  • 43
  • 87
  • A trick I learned from Jon Skeet's book - you can avoid those annoying null checks by declaring your events like this `public event PropertyChangedEventHandler PropertyChanged = delegate {};` – Adam Rackis Dec 25 '11 at 02:10
  • @Adam I know, thanks. There are many diskussions about this, e.g. [Avoid checking for null event handlers](http://stackoverflow.com/a/9282/620360) or [Is there a downside to adding an anonymous empty delegate on event declaration?](http://stackoverflow.com/questions/170907/is-there-a-downside-to-adding-an-anonymous-empty-delegate-on-event-declaration) – LPL Dec 25 '11 at 10:09