如何手动更新多重绑定
我在绑定
方面遇到了问题。 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
,并且上述解决方案停止工作(b
为 null
)。有没有办法强制多重绑定
更新目标属性?
最好的问候——斯普克。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于多重绑定,绑定表达式是
MultiBindingExpression
,它继承自BindingExpressionBase
,但不是继承自BindingExpression
。因此,GetBindingExpression
对于多重绑定返回 null。相反,您可以使用BindingOperations.GetMultiBindingExpression:For a multibinding, the binding expression is a
MultiBindingExpression
, which inherits fromBindingExpressionBase
, but not fromBindingExpression
. SoGetBindingExpression
returns null for a multibinding. Instead you can useBindingOperations.GetMultiBindingExpression
: