What is the best way to synchronize two datasets using binding?
Target = Custom Setters - raises custom events whenever something changed Source = ObservableCollection - raises events whenever collection changed
Now my question is: when I receive an update from one collection (for example, the Source.CollectionChanged event), I need to call custom TargetSetters and ignore the events raised from my update.
And also in another case, when custom Target events are fired, I need to update the source code, but ignore the CollectionChanged event.
At the moment, I keep a link to my handlers and delete it before updating any of the collections. eg.
private void ObservableCollection_OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { CustomObject.SelectionChanged -= CustomObject_SelectionChanged;
I saw that you can use the if statement to check if the updates are source, and if they ignore them. eg.
private void ObservableCollection_OnCollectionChanged2(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { if (BindingTargetUpdating) return; BindingSourceUpdating = true; // Do change logic and update Custom Object.... BindingSourceUpdating = false; } void CustomObject_SelectionChanged2(object sender, SelectionChangedEventArgs e) { if (BindingSourceUpdating) return; BindingTargetUpdating = true; // Do change logic and update ObservableCollection... BindingTargetUpdating = false; }
After Google + SO Search came back with nothing, I wanted to see how other people do it, and is there something really simple, am I missing here that solves this problem? (I know that the examples are not thread safe)
If not, what is the preferred way? Removing and installing handlers or setting a boolean flag? Which is more productive (yes, I know that this is unlikely to cause a bottleneck, but out of curiosity)
The reason I'm asking for is because I am currently implementing Attached Behaviors and for each behavior, I am creating 2 sets of dictionaries that contain references to handlers for each object, since the state must be passed.
I cannot find the source code for the .NET binding class binding mechanism to see how MS implemented it. If anyone has a link to those, this will be very helpful.