Wpf 内容属性是如何工作的
下面的两个语法都有效。我的问题是WPF如何知道内容是我指定的Wpf 内容属性是如何工作的,wpf,xaml,Wpf,Xaml,下面的两个语法都有效。我的问题是WPF如何知道内容是我指定的 <Button> <Button.Content> my button </Button.Content> </Button> <Button> my button <!--how does wpf know this is the content--> </Button> 我的纽扣 我的纽扣 类似地
<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>
项目2
因此,在ContentControl中,默认属性是Content,对于ItemsControl,默认属性是Items,对于TextBox,默认属性是TextBox。“默认”是如何工作的?
创建自定义控件时,如何创建此“默认属性”?告诉xaml解析器直接内容使用哪个属性
ContentControl
,按钮从中继承,标记为attibute[ContentProperty(“Content”)]
。类似地,ItemsControl
(列表框的祖先)用[ContentProperty(“Items”)]
标记