-
- 属性值继承与传统的类的继承不同,它指的是属性值自顶向下沿着元素树传递。
- 并不是每个依赖属性都参与属性值的继承
- 属性的值可能由一些优先级更高的源设置
- 属性值可以被传递给一些并非逻辑树或可视树中的子元素。例如:元素的触发器等。
3.2.3 属性值继承
例如:
<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"
FontSize="30"
FontStyle="Italic"
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 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>
这里对 Window 设置的 FontSize 和 FontStyle 属性将传递到其下元素树的每个元素。由于Label 已经显式设置了 FontSize 的值,这将重载从 Window 继承的 FontSize 的值,因此字体大小与设置的值相同。StatusBar的文本不受任何值的影响,因此其字体大小和类型都不会发生变化。
例如:StatusBar、Menu和ToolTrip控件,其内部会把字体属性设置为当前的系统设置。