如何转换已绑定到多绑定的控件

时间:2013-04-24 17:19:28

标签: wpf binding multibinding

假设我有一个FrameworkElement已绑定到XAML中的转换器。以编程方式,我希望在运行时将绑定附加到现有绑定 - 例如在控件的加载事件中,有效地使其成为多重绑定。

如何获取(例如)Visibility属性的现有绑定。

1 个答案:

答案 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);