Reputation: 109
A very common scenario. We have:
double _H;
public double H
{
get { return _H; }
set
{
if (_H == value) return;
_H = value;
SomeMethod("H");
//Inside SomeMethod's body we calculate some other properties among other things,
//and we call their corresponding base.RaisePropertyChanged. ALSO we
//RECALCULATE the freshly set H and we WANT to call base.RaisePropertyChanged("H")
//to "propagate" the changed value back to the View control that called the setter
//portion of the property in the first place!
}
}
Answer : Look at Jay's post. Key concept to keep from this question: Asynchronous Invocation as Jay mentioned.
Some more details (maybe repetitive or irrelevant) : I have a NumericUpDown control and I click on it's button to change its value. The problem is that I want to recalculate the given value and see if it is permitted (validation in view-model). But I cant push back the value that is being sent from control to the set portion of the property. The first solution which came in mind is to trigger the ValueChanged
event in the View and call SomeMethod("H")
from there. Not good though.
In reality there are about 10 NumericUpDown controls. The value of each represent a dimension of a geometric shape. So, changes in one value, can alter the other dimensions. The problem arises when the calculation determines that the value just given must change also (if you understand what I mean). Also some relevant XAML code:
<l:NumericUpDown Value="{Binding H}" Maximum="{Binding MaxSide, Mode=OneTime}"
Grid.Column="7"/>
Upvotes: 1
Views: 469
Reputation: 57899
You need to use the Dispatcher to either force a refresh or to raise the property change notification outside of the context of the setter (asynchronous invocation).
So, instead of
base.OnPropertyChanged("H");
…you'd do
Action action = () => base.OnPropertyChanged("H");
Dispatcher.CurrentDispatcher.BeginInvoke(action);
Upvotes: 4