I need to update a TextBox that’s bound to a property. In the way I’ve implemented it it’s working fine. Here’s the code
private double foo;
public double Foo
{
get { return foo; }
set
{
foo = value;
RaisePropertyChanged(() => Foo);
}
}
But now I need to update the value of this property from another property and the TextBox bound to Foo is not updated. Here’s the code
private string foo1
public string Foo1
{
get { return foo1; }
set
{
foo1 = value;
foo = 4; // Updating the Foo property indirectly
RaisePropertyChanged(() => Foo);
RaisePropertyChanged(() => Foo1);
}
}
I’m obligated to update the value of the property Foo in that way because Foo and another property are updated each other so I can’t update the properties directly because I fall in an infinity recursion.
The question is How I can update the TextBox that is bound to the Foo property when I change the value of the attribute foo?
I would say you should update the public member
Fooin yourFoo1setter. That will cause theRaisePropertyChangedevent to fire forFoo.You could call
RaisePropertyChanged(() => Foo);whenever you update your private fieldfoo, but unless there is a good reason not to use the propertyFoo, I would always use it overfoo. The intention ofsetis to have that code run whenever the value of the property changes. In my opinion, setting the private field bypasses the code insetwhich violates this intention.EDIT
On another note, if you only want to call
RaisePropertyChangedand change the value ofFoowhenFoo1changes (not necessarily each time the setter is called), just add a check to see if the value has changed. This will take care of your recursion problem.