0503样式定义
1、Setter定义单个样式,指定关联
<Border> <Border.Resources> <Style x:Key="style"> <Setter Property="Border.BorderBrush" Value="red"/> <Setter Property="Border.BorderThickness" Value="2"/> </Style> </Border.Resources> <Border.Style> <StaticResource ResourceKey="style"/> </Border.Style> </Border>
2、TargetType="Border"
指定样式类型
Setter里面的Property可以省略Border.
<Setter Property="BorderBrush" Value="red"/>
<Setter Property="BorderThickness" Value="2"/>
3、有一个Border的Style属性需要一个Style类型的实例
Setter是针对对象的所有属性
可以设置<sys:Double x:Key="value">200</sys:Double>对象资源
Setter引用
<Setter Property="Height" Value="{StaticResource value}"/>
4、属性-资源-属性,行内简便写法,挪到Window.Resources
<Window.Resources> <sys:Double x:Key="value">200</sys:Double> <Style x:Key="style" TargetType="Border"> <Setter Property="BorderBrush" Value="red"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="Height" Value="{StaticResource value}"/> </Style> </Window.Resources> <Grid> <Border Width="{StaticResource value}" Style="{StaticResource style}"> <Image Width="100" Height="100" Source="/Assets/img1.jpg" /> </Border> </Grid>
5、Style.Resources
<Window.Resources> <Style x:Key="style" TargetType="Border"> <Style.Resources> <sys:Double x:Key="value">200</sys:Double> </Style.Resources> <Setter Property="BorderBrush" Value="red"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="Width" Value="{StaticResource value}"/> <Setter Property="Height" Value="{StaticResource value}"/> </Style> </Window.Resources> <Grid> <Border Style="{StaticResource style}"> <Image Width="100" Height="100" Source="/Assets/img1.jpg" /> </Border> </Grid>
6、BasedOn
s1圆角,继承style所有属性,引用只需要使用s1就可以所有效果
<Window.Resources> <Style x:Key="style" TargetType="Border"> <Style.Resources> <sys:Double x:Key="value">200</sys:Double> </Style.Resources> <Setter Property="BorderBrush" Value="red"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="Width" Value="{StaticResource value}"/> <Setter Property="Height" Value="{StaticResource value}"/> </Style> <Style x:Key="s1" TargetType="Border" BasedOn="{StaticResource style}"> <Setter Property="CornerRadius" Value="30"></Setter> </Style> </Window.Resources> <Grid> <Border Style="{StaticResource s1}"> <Image Width="100" Height="100" Source="/Assets/img1.jpg" /> </Border> </Grid>
style必须在s1之前定义,否则报错
7、优先级
如果s1里面也定义了重复的属性,会覆盖父类的属性
如果Border里面也定义了同样的属性,优先级最高,内联样式

标签
<Style TargetType="TextBlock" x:Key="tb">
<Setter Property="FontSize" Value="30"/>
<Setter Property="Background" Value="Red"/>
<Setter Property="Width" Value="100"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="30"/>
<Setter Property="Background" Value="Orange"/>
<Setter Property="Width" Value="100"/>
</Style>
设置了tb就会对tb起作用
<TextBlock Text="Hello" Style="{StaticResource tb}"/>
没有设置就是对所有标签气作用,跟前后优先级无关
浙公网安备 33010602011771号