C# 依附性奇怪的行为

C# 依附性奇怪的行为,c#,wpf,dependency-properties,contentcontrol,C#,Wpf,Dependency Properties,Contentcontrol,我尝试了这个,只是为了看看会发生什么,它确实起了作用,但我不知道为什么会这样。有人能解释一下在DependencyProperties的背景下发生了什么吗 我有一个类,它声明了一个dependencProperty,但在另一个类中,我使用GetValue和SetValue将dependencProperty作为目标 以下是一个例子: public class DependencyProperties : DependencyObject { public Size EstimatedSi

我尝试了这个,只是为了看看会发生什么,它确实起了作用,但我不知道为什么会这样。有人能解释一下在
DependencyProperties
的背景下发生了什么吗

我有一个类,它声明了一个
dependencProperty
,但在另一个类中,我使用
GetValue
SetValue
dependencProperty
作为目标

以下是一个例子:

public class DependencyProperties : DependencyObject
{
    public Size EstimatedSize
    {
        get { return (Size)GetValue(EstimatedSizeProperty); }
        set { SetValue(EstimatedSizeProperty, value); }
    }

    public static readonly DependencyProperty EstimatedSizeProperty =
        DependencyProperty.Register("EstimatedSize", typeof(Size), typeof(DependencyProperties), null);
}

public class MyControl: ContentControl
{
    public Size CalculatedSize
    {
        get { return (Size)GetValue(DependencyProperties.EstimatedSizeProperty); }
        set { SetValue(DependencyProperties.EstimatedSizeProperty, value); }
    }

    protected override OnApplyTemplate()
    {
      // This works but why? How is it possible to do this? What is happening under the hood?
      this.CalculatedSize = new Size(123, 123);
    }
}

为什么可能这样做?在这个例子的背景下发生了什么?MyControl类没有注册DP,但可以使用它。有人能告诉我引擎盖下发生了什么吗?

我在谷歌上搜索了我想给你看的图像。 请参阅下面的链接,DP的概念已经有了很好的文档记录

让我们直接进入底线,当您在应用程序中邀请并使用MyControl时,包含的DP将自动注册。
这就是为什么DP使用静态前缀。由于DP声明中的
静态只读
,请阅读链接(Priyank引用的答案)。

可能重复:如果使用.Register(…rameworkPropertyMetadataOptions.Inherits),会发生什么?它不是附属财产,而是试图继承。