在 XAML 中设置元素的内容时,WPF 如何确定要设置的属性?

在 XAML 中设置元素的内容时,WPF 如何确定要设置的属性?

问题描述:

我希望我的标题很清楚......

I hope my title is clear....

我的意思是例如当我们定义动画时:

What I mean is for exmaple when we define animation:

XML:

<StoryBoard>
    <DoubleAnimation..../>
</StoryBoard>

但是如果我们在代码隐藏中定义相同的东西,我们可以这样做:

But if we defines same thing in code-behind, we could do:

DoubleAnimation myDAnimation = new DoubleAnimation();
.....
StoryBoard myStoryBoard = new StoryBoard();
myStoryBoard.Children.Add(myDAnimation);

我试图查看 StoryBoard 的类定义,没什么特别的:

I tried to look into StoryBoard's class definition, nothing special:

public sealed class Storyboard : Timeline
{
        public Storyboard();

        // Summary:
        //     Gets the collection of child System.Windows.Media.Animation.Timeline objects.
        //
        // Returns:
        //     The collection of child System.Windows.Media.Animation.Timeline objects.
        //     The default is an empty collection.
        public TimelineCollection Children { get; }
....
}

我知道如果我使用上述 sytnax 定义我自己的类,为了添加到我的孩子中,我需要:

I know if I defines my own class with the above sytnax, in order to add into my Children, I would need to:

XAML:

<MyClass>
   <MyClass.Children>
      <MyClassCildrenItem..../>
   </MyClass.Children>
</MyClass>

那么 Xaml 是如何知道 DoubleAnimation 应该添加到 StoryBoardChildren 属性的?如果我需要做同样的事情,我需要申报什么?

So how did the Xaml knows the DoubleAnimation is should add into StoryBoard's Children property? If I need to do the same thing, what do I need to declare?

有一个属性:ContentPropertyAttribute

如果您查看 MSDN 上类的 Syntax 部分,您可以看到指定内容时将设置哪些属性.例如.ContentControlContent 属性为目标.

If you check the Syntax section of classes on MSDN you can see what properties will be set when specifying content. E.g. the ContentControl targets the Content property.