开发者

How to dispatch a new property value in an object to the same property of two other objects

开发者 https://www.devze.com 2022-12-22 17:44 出处:网络
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

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 th开发者_运维问答e 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.


Actually nobody was able to find the solution within 2 weeks, so I believe there is none. Sometime it's like this...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号