如何将对象中的新属性值分配给其他两个对象的相同属性

发布于 2024-08-25 01:31:57 字数 1076 浏览 11 评论 0原文

在 WPF 中,我有三个对象公开相同的 DependencyProperty(假设它是一个整数)。我希望所有三个属性值保持同步,即每当对象中的 int 值发生更改时,该值都会传播到其他两个对象。我认为使用多重绑定来完成这项工作,但我不知道如何检测哪个对象发生了变化,从而应该使用哪个值并将其传播到其他对象。

已编辑:这是我的多重绑定的暂定代码,错误地希望它无需额外代码即可工作:

// 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);

但实际上,存在运行时错误,表示最后一条指令设置的绑定缺少转换器。但我又不知道如何编写这个转换器(没有什么可转换的(因为在将 3 个 rgb 属性链接到颜色属性的相关 MS 代码示例中就是这种情况),只能转发更改的属性值到另外两个属性)。

我知道我可以通过在 3 种类型中创建 X_Changed 事件来解决问题,然后让每个对象注册到其他两个对象事件。我不喜欢这种“手动”方式,更喜欢将 3 个属性绑定在一起。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2024-09-01 01:31:57

事实上,两周内没有人能够找到解决方案,所以我相信没有人能找到解决方案。有时候是这样的...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文