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

时间:2012-03-18 19:36:13

标签: c# wpf silverlight xaml

我希望我的头衔清楚......

当我们定义动画时,我的意思是exmaple:

的Xaml:

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

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

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

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

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定义我自己的类,为了加入我的孩子,我需要:

XAML:

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

那么Xaml是如何知道DoubleAnimation应该添加到StoryBoard的{​​{1}}属性中的呢? 如果我需要做同样的事情,我需要申报什么?

2 个答案:

答案 0 :(得分:5)

有一个属性:ContentPropertyAttribute

如果您检查MSDN上的类的Syntax部分,您可以看到在指定内容时将设置哪些属性。例如。 ContentControl定位Content属性。

答案 1 :(得分:1)

TimelineGroup Storyboard的祖先具有Children属性,并且还ContentPropertyAttribute指定Children属性作为内容属性。