How to dispatch a new property value in an object to the same property of two other objects
Posted
by WPFadvocate
on Stack Overflow
See other posts from Stack Overflow
or by WPFadvocate
Published on 2010-03-14T11:51:46Z
Indexed on
2010/03/15
1:19 UTC
Read the original article
Hit count: 446
multibinding
|wpf
In WPF, I've three objects exposing the same DependencyProperty
(let's say it's an integer). I want all three property values to remain synchronized, i.e. that whenever the int value changes in an object, this value is propagated to the two other objects. I think of multibinding
to do the job, but I don't know how to detect which object changed, thus which value should be used and propagated to the other objects.
Edited: here is my tentative code for multibinding, with the false hope that it would work without additional code:
// create the multibinding
MultiBinding mb = new MultiBinding() {
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
// create individual bindings to associate object_2 and object_3 to object_1
Binding b2 = new Binding() {
Source = object_2,
Path = new PropertyPath("X")
};
Binding b3 = new Binding() {
Source = object_3,
Path = new PropertyPath("X")
};
// add individual bindings to multibinding
mb.Bindings.Add(b2);
mb.Bindings.Add(b3);
// bind object_2 and _3 to object_1
BindingOperations.SetBinding(object_1, TypeObject_1.XProperty, mb);
But actually, there is a runtime error, saying the binding set by the last instruction is lacking a converter. But again I don't know how to write this converter (there is nothing to convert (as this is the case in the related MS sample of code linking 3 rgb properties to a color property), only to forward the value of the property changed to the two other properties).
I understand I could solve the problem by creating an X_Changed event in the 3 types and then have each object registering to the two other objects event. I don't like this "manual" way and would prefer to bind the 3 properties together.
© Stack Overflow or respective owner