在 XAML 中设置元素的内容时,WPF 如何确定要设置的属性? [英] How does WPF determine what property to set when setting the content of elements in XAML?

查看:26
本文介绍了在 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.

这篇关于在 XAML 中设置元素的内容时,WPF 如何确定要设置的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆