简体   繁体   English

在XAML元素上绑定CRL包装器或绑定直接依赖项属性

[英]bind CRL wrapper or bind direct dependency property on XAML element

I have simple depedency property in window. 我在窗口中有简单的依赖属性。

    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);
        }
    }

When I bind direct depedency property on textBox binding doesn't work. 当我在textBox上绑定直接依赖属性时,绑定不起作用。

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

But when I bind CLR prop wrapper on textBox binding works. 但是当我在textBox上绑定CLR prop包装器时,绑定工作正常。

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

Why I can't bind direct depedency property on textBox? 为什么我不能在textBox上绑定直接依赖属性?

You are confusing static DependencyPropertyIdentifier with instance CLR wrapper for that property. 您将static DependencyPropertyIdentifier与该属性的instance CLR wrapper混淆。

DependencyPropertyIdentifier is static field which is register at class level and embed in class metadata. DependencyPropertyIdentifier是静态字段,在类级别注册并嵌入到类元数据中。 Whereas to fetch and set the value for an instance, GetValue() and SetValue() is called on that DP identifier. 要获取并设置实例的值,则在该DP标识符上调用GetValue()SetValue()

From MSDN - MSDN-

  1. Dependency property identifier : A DependencyProperty instance, which is obtained as a return value when registering a dependency property, and then stored as a static member of a class. 依赖项属性标识符 :DependencyProperty实例,该实例在注册依赖项属性时作为返回值获取,然后存储为类的静态成员。 This identifier is used as a parameter for many of the APIs that interact with the WPF property system. 此标识符用作与WPF属性系统进行交互的许多API的参数。
  2. CLR "wrapper" : The actual get and set implementations for the property. CLR“包装” :该属性的实际get和set实现。 These implementations incorporate the dependency property identifier by using it in the GetValue and SetValue calls, thus providing the backing for the property using the WPF property system. 这些实现通过在GetValue和SetValue调用中使用依赖项标识符来合并依赖项标识符,从而使用WPF属性系统为该属性提供支持。

Dependency properties on a given type are accessible as a storage table through the property system. 可以通过属性系统将给定类型的依赖项属性作为存储表进行访问。 Instance value is stored in that storage table and WPF implementation of XAML processor uses that table to get and set value for an instance object. 实例值存储在该存储表中,XAML处理器的WPF实现使用该表来获取和设置实例对象的值。

I would suggest you to read more about it here and here . 我建议您在这里这里阅读更多有关它的信息

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

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