【问题标题】:bind CRL wrapper or bind direct dependency property on XAML element在 XAML 元素上绑定 CRL 包装器或绑定直接依赖项属性
【发布时间】:2013-11-13 13:50:02
【问题描述】:

我在窗口中有简单的依赖属性。

    public static readonly DependencyProperty UserLastNameProperty =
        DependencyProperty.Register("UserLastName",
            typeof (string),
            typeof (MainWindow),
            new FrameworkPropertyMetadata(default(string),
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public string UserLastName
    {
        get
        {
            return (string) GetValue(UserLastNameProperty);
        }
        set
        {
            SetValue(UserLastNameProperty, value);
        }
    }

当我在 textBox 上绑定直接依赖属性时,绑定不起作用。

        <TextBox Margin="4" FontSize="14" x:Name="TxbLastName" MinWidth="200"
                 Text="{Binding UserLastNameProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

但是当我在 textBox 绑定上绑定 CLR 属性包装器时。

        <TextBox Margin="4" FontSize="14" x:Name="TxbLastName" MinWidth="200"
                 Text="{Binding UserLastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

为什么我不能在 textBox 上绑定直接依赖属性?

【问题讨论】:

    标签: c# wpf binding dependency-properties


    【解决方案1】:

    您将该属性的 static DependencyPropertyIdentifierinstance CLR wrapper 混淆了。

    DependencyPropertyIdentifier 是静态字段,在类级别注册并嵌入到类元数据中。而要获取和设置实例的值,则在该 DP 标识符上调用 GetValue()SetValue()

    来自MSDN -

    1. 依赖属性标识符:一个DependencyProperty实例,注册依赖时作为返回值获取 属性,然后存储为类的静态成员。这 标识符用作许多交互的 API 的参数 使用 WPF 属性系统。
    2. CLR "wrapper":属性的实际 get 和 set 实现。这些实现包含依赖属性 在 GetValue 和 SetValue 调用中使用标识符,因此 使用 WPF 属性系统为属性提供支持。

    给定类型的依赖属性可以通过属性系统作为存储表进行访问。实例值存储在该存储表中,XAML 处理器的 WPF 实现使用该表来获取和设置实例对象的值。

    我建议您阅读更多关于它的信息 herehere

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 2014-01-09
      • 2023-03-13
      • 2011-12-20
      • 2017-03-04
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多