質問

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 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 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?

役に立ちましたか?

解決

You are confusing static DependencyPropertyIdentifier with instance CLR wrapper for that property.

DependencyPropertyIdentifier is static field which is register at class level and embed in class metadata. Whereas to fetch and set the value for an instance, GetValue() and SetValue() is called on that DP identifier.

From 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. This identifier is used as a parameter for many of the APIs that interact with the WPF property system.
  2. CLR "wrapper": The actual get and set implementations for the property. 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.

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.

I would suggest you to read more about it here and here.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top