假设这里有一个全局的资源
- <Style TargetType="TextBlock">
- <Setter Property="FontSize" Value="40"/>
- Style>
这是时候有些控件可能需要一个样式在这个基础上加一点内容的
- <Style x:Key="textBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
- <Setter Property="Foreground" Value="Yellow"/>
- Style>
--其实这个TargetType="TextBlock"是TargetType="{x:Type TextBlock}"的简写
如果是普通有key的就basedon后面接静态资源就行了
- <Style x:Key="textBlockStyle1" TargetType="TextBlock" BasedOn="{StaticResource textBlockStyle}">
- <Setter Property="Foreground" Value="Yellow"/>
- Style>
拓展:Sytle设置TargetType的作用就是方便给Setter设置属性,因为不同控件有不同的属性。要是没有用TargetType指定是哪个控件,Setter设置属性的时候需要加上控件的限定名:“TextBlock.Property="TextBlock.Foreground”。