-
- 附加属性是依赖属性的一种特殊形式,可以被有效地添加到任何对象中。
- 附加属性通常都是用于用户界面元素的布局。
- 附加属性可以高效地向密封类的实例添加自定义数据。
- 通过定义依赖属性的方法,可以把任何一个依赖属性作为一个附加属性。
3.2.5 附加属性
例如:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About WPF Unleashed"
SizeToContent="WidthAndHeight"
Background="OrangeRed">
<StackPanel>
<Label FontWeight="Bold"
FontSize="20"
Foreground="White">
WPF Unleashed (Version 3.0)
</Label>
<Label>
© 2006 SAMS Publishing
</Label>
<Label>
Installed Chapters:
</Label>
<ListBox>
<ListBoxItem>
Chapter 1
</ListBoxItem>
<ListBoxItem>
Chapter 2
</ListBoxItem>
</ListBox>
<StackPanel TextElement.FontSize="30"
TextElement.FontStyle="Italic"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Button MinWidth="75"
Margin="10">
Help
</Button>
<Button MinWidth="75"
Margin="10">
OK
</Button>
</StackPanel>
<StatusBar>
You have successfully registered this product.
</StatusBar>
</StackPanel>
</Window>
其中:TextElement.FontSize和TextElement.FontStyle必须在StackPanel元素中使用。因为StackPanel元素没有FontSize和FontStyle两个属性。
其过程式代码可以表示为:
StackPanel pannel = new StackPanel();
TextElement.SetFontSize(panel, 30);
TextElement.SetFontStyle(panel, FontStyle.Italic);
例如:
GeometryModel3D model = new GeometryModel3D();
model.SetValue(FrameworkElement.TagProperty, "my custom data");
例如:
okButton.SetValue(ListBox.IsTextSearchEnabledProperty, true);