Verify Pending Changes When Using WPF Explicit Binding

I have a modal popup containing a CheckBox that uses an explicit linking link to update the link source when the user clicks the save button on the form.

<CheckBox Content="Default" IsChecked="{Binding Path=Unit.IsDefault, Mode=TwoWay, UpdateSourceTrigger=Explicit"/> 

Now I want to add a cancel button to the form, if the user clicks it, I would like to check if there are any pending binding updates, and if so, show the user a message.

Can this be done with bindings? I hope for something like:

 BindingExpression binding = cb.GetBindingExpression(CheckBox.IsCheckedProperty); binding.HasPendingUpdates(); // Anything similar to this? 

Otherwise, does anyone have any other suggestions on how to track binding changes that have not yet been explicitly updated?

+4
source share
3 answers

As IsDirty noted, the IsDirty on BindingExpressionBase can be used if you are using .NET 4.5.

In the meantime, a workaround could be to check the property of the internal NeedsUpdate using reflection:

 public static bool IsDirty(this BindingExpression binding) { if (binding == null) throw new ArgumentNullException("binding"); var needsUpdateProperty = typeof(BindingExpressionBase).GetProperty("NeedsUpdate", BindingFlags.Instance | BindingFlags.NonPublic); var isDirtyAsObject = needsUpdateProperty.GetValue(binding, null); if (isDirtyAsObject is bool) return (bool)isDirtyAsObject; return false; } 
+1
source

You can use the converter. Add the IsSuspended and HasPendingChanges property to the converter. When IsSuspended is true, return Binding.DoNothing () from the converter and set HasPendingChanges to true inside the converter. Otherwise, return the original value and set HasPendingChanges to false.

When declaring this converter in the xaml set, IsSuspended to true. When the user clicks the button, first check HasPendingChanges, and then set IsSuspended to false and update the binding. after the update, return IsSuspended to true.

0
source

defaultCheckBox.GetBindingExpression (CheckBox.IsCheckedProperty) .IsDirty

0
source

Source: https://habr.com/ru/post/1387024/


All Articles