Style 控件样式
<Window.Resources> <Style x:Key="BaseButtonStyle" TargetType="Button"> <Setter Property="FontSize" Value="15"/> </Style> <Style x:Key="myStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> <Setter Property="Background" Value="Blue"/> <Setter Property="Foreground" Value="White"/> </Style> </Window.Resources> <Grid> <StackPanel> <Button Style="{StaticResource myStyle}" Content="Button" /> <Button Style="{StaticResource myStyle}" Content="Button" /> <Button Content="Button" /> </StackPanel> </Grid>
统一设置Grid控件中某类元素的样式
1 <Grid Background="Beige" ShowGridLines="True"> 2 <!-- ShowGridLines for testing only --> 3 <Grid.Resources> 4 <Style TargetType="TextBlock"> 5 <Setter Property="Margin" Value="3"/> 6 </Style> 7 </Grid.Resources> 8 <Grid.ColumnDefinitions> 9 <ColumnDefinition Width="Auto" /> 10 <ColumnDefinition /> 11 </Grid.ColumnDefinitions> 12 <Grid.RowDefinitions> 13 <RowDefinition Height="Auto"/> 14 <RowDefinition Height="Auto"/> 15 <RowDefinition Height="Auto"/> 16 </Grid.RowDefinitions> 17 <TextBlock Grid.Column="0" Grid.Row="0">Protocol:</TextBlock> 18 <TextBlock Grid.Column="1" Grid.Row="0">HyperText Transfer Protocol</TextBlock> 19 <TextBlock Grid.Column="0" Grid.Row="1">Type:</TextBlock> 20 <TextBlock Grid.Column="1" Grid.Row="1">HTML Document</TextBlock> 21 <TextBlock Grid.Column="0" Grid.Row="2">Connection:</TextBlock> 22 <TextBlock Grid.Column="1" Grid.Row="2">Not encrypted</TextBlock> 23 </Grid>