Content属性是如何工作的?

3

以下两种语法都可以使用。我的问题是WPF如何知道Content是我指定的内容?

<Button>
    <Button.Content>
        my button
    </Button.Content>
</Button>

<Button>
     my button  <!--how does wpf know this is the content-->
</Button>

同样地,WPF 如何知道我现在正在添加 ListBoxItems?
<ListBox>
    <!--ListBox.Items-->  
        <ListBoxItem Content="item 1" />  <!--XAML automatically knows I'm specifying items-->
        item 2
        <ListBoxItem Content="item 3" />
    <!--/ListBox.Items-->
</ListBox>

在ContentControl中, 默认属性是 Content,在ItemsControl中,默认属性是 Items,在TextBox中,默认属性是 TextBox。
这个“默认”是怎么工作的呢?
当我创建自定义控件时,如何创建这个“默认属性”?

1个回答

3
< p > ContentPropertyAttribute 告诉 XAML 解析器使用哪个属性作为直接内容。

ContentControl,从中继承了Button,被标记为属性[ContentProperty("Content")]。同样,ItemsControlListBox的祖先)被标记为[ContentProperty("Items")]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接