Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for WPF Binding Delay to complete

Tags:

c#

binding

wpf

My ViewModel implements the INotifyPropertyChanged and INotifyDataErrorInfo interfaces. When the property is changed, the validation triggers, which in turn enables\disable the Save button.

Because the Validation step is time consuming I've made use of the Delay binding property.

My problem is that I can type my changes and press Save before the 'Name' property is updated.

I'd like to force an immediate update on the TextBox.Text when I press SaveChanges. At the moment, I have to add a sleep before executing to ensure all changes have occurred on the ViewModel.

<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Delay=1000}" />
<Button Command="{Binding SaveChanges}" />

Does anyone have some pointers?

like image 764
Rob Dunbar Avatar asked Nov 01 '22 01:11

Rob Dunbar


2 Answers

Since .NET 4.5 there exists BindingOperations

BindingOperations.GetSourceUpdatingBindings(this).ToList().ForEach(x => x.UpdateSource());
like image 85
jaz Avatar answered Nov 15 '22 05:11

jaz


You can implement the IPropertyChanged interface on your viewModel, and then from your Name property setter to check if the value has changed, and raise an OnPropertyChanged event for that property.

You can use that property changed event to wire up your SaveChanges command CanExecute method to return false if not updated yet, and return true if the delay is elapsed and the property is updated.

Therefore, the SaveChanges button stays disabled until the CanExecute returns true.

like image 26
Lin Song Yang Avatar answered Nov 15 '22 06:11

Lin Song Yang