例如,下面代码中的FontSize,会应用到所有子控件中去,但是,背景色却不能,为什么?有什么办法可以使背景色,应用到所有子控件?
<Window x:Class="Search_On_Word_2010_Docx.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window" FontSize="50" Background="BlUe">
<Window x:Class="Search_On_Word_2010_Docx.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window" FontSize="50" Background="BlUe">
皮肤样式是由Windows Presentation Foundation中的控件基类(FrameworkElement、FrameworkContentElement)中的换肤机制,使得程序的逻辑层和表示层独立!也是XAML实现“面向对象编程”思路的方法,一个控件中Style属性包含着一个Style对象,当Style对象更改时就可更新控件的大小及颜色等属性。Style对象对表示层中还包括鼠标上去事件时的变化,和动画等的支持!这一系列数据组成一个Style对象,并可以把这个Style对象应用到多个控件。
XAML对Style的支持很好,一般请况Style初始化在父控件的Resources标记里面做为一个资源等待调用。例如: <Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPFHOME Application" Height="100" Width="359"> <Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="style2">
<Setter Property="Background" Value="Red"/>
</Style>
</Window.Resources>
<DockPanel>
<Button Name="button1" Width="179.5">Button1</Button>
<Button Name="button2" Style="{StaticResource style2}">Button2</Button>
</DockPanel>
</Window> 其中Style节在父控件Window的Resources属性中,表意Window下所有的子控件都可以获取Window.Resources中的对象。其中第一个Style没有制定x:Key属性,这样会作为默认样式,应用到全部的没套用Button类型的控件上。而style2样式中使用了BaseOn属性继承了默认皮肤中的Foreground,而且重写了Background。如果第1个style不是默认Style制定了x:key="style1",第2个Style中的BaseOn应改为BaseOn={StaticResource style1}同样可以继承第一个皮肤。参考http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/50754eac-aa63-41d1-9af5-b1f12892edef/