如何手动更新多重绑定

发布于 2024-10-31 02:27:20 字数 700 浏览 4 评论 0原文

我在绑定方面遇到了问题。 Rectangle.Fill 依赖属性已通过转换器绑定到 ObservableCollection。尽管 ObservableCollection 实现了 INotifyCollectionChanged,但绑定并未更新。不过,我设法通过将我的委托附加到集合的更改通知事件并手动刷新绑定来解决此问题:

    void ColorsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        BindingExpression b = colorRectangle.GetBindingExpression(Rectangle.FillProperty);
        if (b != null)
            b.UpdateTarget();
    }

但是,最近我将 Binding 更改为 MultiBinding,并且上述解决方案停止工作(bnull)。有没有办法强制多重绑定更新目标属性?

最好的问候——斯普克。

I had a problem with the Binding. The Rectangle.Fill dependency property was bound to an ObservableCollection with the converter. Although the ObservableCollection implements INotifyCollectionChanged, the binding was not updated. I managed, however, to solve this by attaching my delegation to the collection's change notification event and refreshing the binding manually:

    void ColorsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        BindingExpression b = colorRectangle.GetBindingExpression(Rectangle.FillProperty);
        if (b != null)
            b.UpdateTarget();
    }

Lately, however, I changed the Binding to MultiBinding, and the above solution stopped working (the b is null). Is there a way to force the Multibinding to update the target property?

Best regards -- Spook.

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

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

发布评论

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

评论(1

如梦 2024-11-07 02:27:20

对于多重绑定,绑定表达式是 MultiBindingExpression,它继承自 BindingExpressionBase,但不是继承自 BindingExpression。因此,GetBindingExpression 对于多重绑定返回 null。相反,您可以使用BindingOperations.GetMultiBindingExpression:

MultiBindingExpression b = BindingOperations.GetMultiBindingExpression(colorRectangle, Rectangle.FillProperty);

For a multibinding, the binding expression is a MultiBindingExpression, which inherits from BindingExpressionBase, but not from BindingExpression. So GetBindingExpression returns null for a multibinding. Instead you can use BindingOperations.GetMultiBindingExpression:

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