WPF基础入门-Class2-样式

WPF基础入门

Class2:样式

1、内联样式:优先度最高

<Grid>
        <StackPanel>
            
            <Button 
                Background="Red" 
                Height="10" 
                Width="100"
                FontSize="20"
                Content="SB">
            Button>
        StackPanel>
    Grid>

2、创建公共Style样式资源(eg.前端的css)

<Window.Resources>
        
        
        <Style x:Key="style_1" TargetType="Button">
            "FontSize" Value="18">
            "Foreground" Value="Blue">
            "Background" Value="DimGray">
            "Height" Value="40">
            "Content" Value="BaseBtn">
        Style>
Window.Resources>
<Grid>
        <StackPanel>
            <Button Style="{StaticResource style_1}">Button>
        StackPanel>
Grid>

3、基于Style,继承资源(Style2继承Style1)

    <Window.Resources>
        
        
        <Style x:Key="style_1" TargetType="Button">
            "FontSize" Value="18">
            "Foreground" Value="Blue">
            "Background" Value="DimGray">
            "Height" Value="40">
            "Content" Value="BaseBtn">
        Style>

        <Style x:Key="child" TargetType="Button" BasedOn="{StaticResource style_1}">
            "Content" Value="继承资源">
        Style>
    Window.Resources>
    
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource style_1}">Button>
            <Button Style="{StaticResource child}">Button>
        StackPanel>
    Grid>

效果图:
WPF基础入门-Class2-样式_第1张图片

你可能感兴趣的:(WPF基础入门,wpf,c#)