假设我有一个FrameworkElement,它已经绑定到XAML中的转换器。在编程上,我希望在运行时将绑定附加到现有绑定中-例如在控件的加载事件中,有效地使其成为多绑定。
如何获取现有的绑定(例如,Visibility属性)
你可以这样做:
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);