绑定继承的依赖属性

时间:2013-09-13 08:49:02

标签: c# wpf data-binding dependency-properties

我有一个可继承的附加属性:

public static readonly DependencyProperty ResourcePackageProperty =
            DependencyProperty.RegisterAttached("ResourcePackage", typeof(IResourcePackage), typeof(ResourceUIElement),
                                                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

在容器控件上,我设置了这个附加属性=>后代控制继承了迄今为止没有的这个。

现在我尝试定义这样的绑定:

var binding = new Binding();
binding.Source = proxy;
binding.Mode = BindingMode.OneWayToSource;
binding.Path = new PropertyPath("Value");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(childControl, ResourceUIElement.ResourcePackageProperty, binding);

childControl 的ResourcePackage属性发生更改时,应该更新代理 - 对象的 Value - 属性。

当我直接在子控件上设置附加属性时,这是有效的,但是当容器控件上的附加属性发生更改时工作(因此,继承到 childControl )。

这对WPF的依赖属性系统是否可行?

1 个答案:

答案 0 :(得分:0)

现在我找到了解决方案:

var binding = new Binding();
binding.Source = childControl;
binding.Mode = BindingMode.OneWay;
binding.Path = new PropertyPath("(0)", ResourceUIElement.ResourcePackageProperty);
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(proxy, Proxy.ValueProperty, binding);

将绑定转向相反的方向。以前它是OneWayToSource,现在Proxy-class派生自DependencyObject,绑定是OneWay。