Consider the following ViewModel property:
private string _slowProperty;
public string SlowProperty
{
get { return _slowProperty; }
set
{
_slowProperty = value;
RaisePropertyChanged("SlowProperty");
}
}
Which is bound to a textbox, like so:
<TextBox Text="{Binding SlowProperty}" />
Now, the problem here is that every time the value of SlowProperty changes, and it does so quite often, the textbox would go and try to get its value, which is, quite slow. I could alleviate the situation using async binding, however, that would still be wasting CPU cycles.
Instead, what I’d like to have is something like:
<TextBlock Text="{z:DelayedSourceBinding SlowProperty}" />
Which would try to get the binding after a certain delay. So for example if the SlowPropertychanged 5 times in a row, within a short while, then only the last text would be visible in the textbox.
I’ve found the following project that performs something like that, so it my example I could use it like so:
<TextBox Text="{z:DelayBinding Path=SearchText}" />
The problem with it, is that it only updates the binding target after a delay. The source path, however, is evaluated and its getter is executed upon every change of the source. Which, in the case of SlowProperty would still waste CPU cycles.
I’ve tried to make my own delayed binding class, but got stuck. Is there any other binder that can do anything like that?
For completeness sake, here are 2 other projects that perform similar tasks, yet, none address the problem I am experiencing:
DeferredBinding – A similar solution to DelayBinding. However, it is a bit more complex to use.
DelayedBindingTextBox – Implements delayed binding using a custom textbox control.
Thanks!
Why not solve this problem in the view model? If your property changes rapidly, but is slow to get, you could have a second ‘delayed’ property exposed by your view model. You could use a timer to update this ‘delayed’ property periodically.
Or, a cleaner implementation could use the Throttle function provided by the reactive extensions framework.