内容属性

来源:互联网 发布:80年代知乎 编辑:程序博客网 时间:2024/06/02 15:41

大多数WPF类都指定了一个属性Content,该属性可以被设置为XML元素中的任何内容。这个属性叫内容属性,通过它,让XAML呈现变得更简单。

看看以下两种方式:

<Button Width="40" Height="40">
OK
</Button>

<Button Width="40" Height="40" Content="OK">
</Button>

使用内容属性还可实现更复杂的Button:

<Button Width="40" Height="40" >
<Button.Content>
<Rectangle Width="20" Height="20" Fill="Yellow"/>
</Button.Content>
</Button>

以上XAML代码等价于:

<Button Width="40" Height="40" >
<Rectangle Width="20" Height="20" Fill="Yellow"/>
</Button>

内容属性并不一定命名为Content,ComboBox、ListBox、TabControl这些类使用Items属性作为它们的内容属性。

原创粉丝点击