简体   繁体   English

DepenencyProperty AddOwner是否包括FrameworkPropertyMetadata?

[英]Does DepenencyProperty AddOwner include FrameworkPropertyMetadata?

When a DependencyProperty that has FrameworkPropertyMetadataOptions.BindsTwoWayByDefault set is reused in a different control via AddOwner(typeof(NewOwner)) does this reuse include the two way binding behavior or should I set it like AddOwner(typeof(NewOwner, new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)) . 当具有FrameworkPropertyMetadataOptions.BindsTwoWayByDefault设置的DependencyProperty通过AddOwner(typeof(NewOwner))在不同的控件中重用时,此重用包括双向绑定行为,还是应将其设置为AddOwner(typeof(NewOwner, new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))

I would expect it does but somehow it looks like it doesn't. 我希望它确实可以,但是某种程度上看起来却没有。 When you check the metadata using GetMetadata on the property I can't check it for the added owner. 当您使用属性上的GetMetadata检查元数据时,无法为添加的所有者检查它。

See this example: 请参阅以下示例:

public class MyControl : Control
{
    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty = ListBox.SelectedItemProperty.AddOwner(typeof(MyControl));
}

then inspect using: 然后使用以下命令进行检查:

  var x = ListBox.SelectedItemProperty.GetMetadata(typeof(ListBox)) as FrameworkPropertyMetadata;
  bool bx = x.BindsTwoWayByDefault;  // true;
  var y = MyControl.SelectedItemProperty.GetMetadata(typeof(MyControl)) as FrameworkPropertyMetadata;
  bool by = y.BindsTwoWayByDefault; // null pointer excpetion

Per documentation : 每个文档

Adds another type as an owner of a dependency property that has already been registered, providing dependency property metadata for the dependency property as it will exist on the provided owner type . 添加另一种类型作为已注册的依赖项属性的所有者,为依赖项属性提供依赖项属性元数据,因为它将存在于提供的所有者类型上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM