加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

与私有CLR属性相比,与私有依赖项属性绑定的工作方式不同

发布时间:2020-12-14 04:58:38 所属栏目:百科 来源:网络整理
导读:我有一个窗口,DataContext设置为自己这个简单的XAML布局 – StackPanel TextBlock Text="{Binding NameCLR}"/ TextBlock Text="{Binding NameDP}"//StackPanel 在代码后面我有两个属性NameCLR – CLR属性和NameDP – 依赖属性. private string NameCLR { get
我有一个窗口,DataContext设置为自己这个简单的XAML布局 –

<StackPanel>
   <TextBlock Text="{Binding NameCLR}"/>
   <TextBlock Text="{Binding NameDP}"/>
</StackPanel>

在代码后面我有两个属性NameCLR – CLR属性和NameDP – 依赖属性.

private string NameCLR
    {
        get { return "CLRProperty"; }
    }

    private string NameDP
    {
        get { return (string)GetValue(NameDPProperty); }
        set { SetValue(NameDPProperty,value); }
    }

    private static readonly DependencyProperty NameDPProperty =
        DependencyProperty.Register("NameDP",typeof(string),typeof(MainWindow),new UIPropertyMetadata("DPProperty"));

由于代码隐藏是部分类定义,而部分是XAML.所以,我假设私有财产应该对XAML可见.但令我惊讶的是,CLR和DP表现不同.

Private Dependency property is accessible but private CLR property
isn’t.

我把输出作为 –

DPProperty

代替

CLRProperty
DPProperty

有人能让我知道DP和CLR属性中的这种不同行为吗?

解决方法

绑定属性由Binding访问,而不是由声明类访问.像NameCLR这样的私有CLR属性是不可访问的,因此Binding将不起作用.

但是,在解析属性路径NameDP时,Binding显然会绕过该属性的CLR包装器并直接访问底层依赖项属性,该属性通过调用DependencyProperty.Register向依赖项属性系统注册.是否已将返回的DependencyProperty引用分配给类中的私有或公共静态字段无关紧要.依赖属性已为您的类注册,因此可以查找它.

来自链接here –

Dependency properties on a given type are accessible as a storage table through the property system,the WPF implementation of its XAML processor uses this table and infers that any given property ABC can be more efficiently set by calling SetValue on the containing DependencyObject derived type,using the dependency property identifier ABCProperty.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读