每个控件都有各自的样式,如果需要重复的利用就把公共样式抽出来。和css一样。

    1. <Window.Resources>
    2. <Style x:Key="AStyle" TargetType="Button">
    3. <Setter Property="Height" Value="30"></Setter>
    4. <Setter Property="Width" Value="60"></Setter>
    5. </Style>
    6. <Style x:Key="BStyle" TargetType="Button" BasedOn="{StaticResource AStyle}">
    7. <Setter Property="FontSize" Value="20"></Setter>
    8. </Style>
    9. <Style x:Key="CStyle" TargetType="Button" BasedOn="{StaticResource BStyle}">
    10. <Setter Property="Foreground" Value="blue"></Setter>
    11. </Style>
    12. </Window.Resources>
    13. <Grid>
    14. <StackPanel>
    15. <Button Style="{StaticResource BStyle}">按钮1</Button>
    16. <Button Style="{StaticResource BStyle}">按钮2</Button>
    17. <Button Style="{StaticResource CStyle}">按钮3</Button>
    18. <Button Style="{StaticResource CStyle}">按钮4</Button>
    19. </StackPanel>
    20. </Grid>

    上面Grid里面放的内容,有4个按钮。用Style属性,打个花括号,花括号里面两个值StaticResource表示静态资源,空一格,写样式名。

    在Grid的同级,Window标签的子级里面写标签,里面写