假设我有一个FrameworkElement已绑定到XAML中的转换器。以编程方式,我希望在运行时将绑定附加到现有绑定 - 例如在控件的加载事件中,有效地使其成为多重绑定。
如何获取(例如)Visibility属性的现有绑定。
答案 0 :(得分:1)
您可能会这样做:
var oldBinding = BindingOperations.GetBinding(element, UIElement.VisibilityProperty);
var newBinding = new MultiBinding { Converter = ... };
var firstBinding = new Binding
{
Path = oldBinding.Path,
Source = oldBinding.Source // or RelativeSource or ElementName
};
var secondBinding = new Binding
{
...
};
newBinding.Bindings.Add(firstBinding);
newBinding.Bindings.Add(secondBinding);
BindingOperations.SetBinding(element, UIElement.VisibilityProperty, newBinding);