How does data binding preclude recursive updating in WPF?

I study binding in WPF, then I have a question:

let's say that the dependency property is tied to the property of an object that implements the INotifyPropertyChanged interface.

when the binding target updates the source, then the source property is updated.

since the setter property of the original object has changed, it, in turn, will inform the listener-target of the binding, which will lead to a recursive update.

How to avoid this in WPF?

+6
data-binding recursion wpf
source share
1 answer

Source updates caused by a property-modified event do not trigger a binding, and property-changing events that occur during a source-binding update are ignored.

Here is an easy way to demonstrate this. Create this class:

public class TestClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private int _Property; public int Property { get { return _Property; } set { if (value < 1000) // just to be on the safe side { _Property = value + 1; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Property")); } } } } } 

Now create a window containing two TextBox es whose Text bound to the Property property on the instance of this class. Make both options two-way binding, and UpdateSourceTrigger - PropertyChanged .

Whenever you enter a number in the limited string of a TextBox , another TextBox displays the next number. The binding of the first TextBox ignores the PropertyChanged event, which the source raises because this event occurs during the binding. The second text field is updated by the first PropertyChanged event, but it does not update the source with its new value.

If you update a property in code (for example, in a button click event), both TextBox es will display the same value - for example. if you set the property to 20, both will be displayed 21. An event with a property change is fired when the property is set to 20, the current value of 21 is displayed, and the bindings do not update the source.

0
source share

All Articles