簡體   English   中英

綁定繼承的依賴項屬性

[英]Binding on inherited Dependency Property

我有一個可繼承的附加屬性:

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的依存屬性系統完全可以做到嗎?

現在我找到了解決方案:

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,現在代理類是從DependencyObject派生的,綁定是OneWay。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM