WPF 学习之控件样式

本文介绍了如何在Windows Presentation Foundation (WPF)中为按钮创建个性化样式,包括单个样式设置、样式复用、样式继承以及样式应用的最佳实践。通过实例演示了如何设置字体大小、前景颜色和内容,并展示了如何避免样式冲突以确保样式正确应用。
摘要由CSDN通过智能技术生成

控件的样式

可以向窗口中添加一个按钮,然后对其样式进行改变。

    <Grid>
        <Button FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />

    </Grid>

需要多个按钮时,可以使用panel

        <StackPanel>
            <Button FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />
            <Button FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />
            <Button FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />
        </StackPanel>

当有很多按钮控件需要使用同样样式的时候,可以创建一个样式

如何创建样式:

创建样式需要在下 的 并且需要给每个样式下一个键值:

    <Window.Resources>
        <Style x:Key="defaultButtonStyle" TargetType="Button">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="red"/>
            <Setter Property="Content" Value="hello"/>

        </Style>
    </Window.Resources>  
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource defaultButtonStyle}" FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />
            <Button Style="{StaticResource defaultButtonStyle}" FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />
            <Button Style="{StaticResource defaultButtonStyle}" FontSize="30" Foreground="Blue" Content="press" Width="200" Height="40" />

        </StackPanel>
    </Grid>

需要注意,当引用样式的时候,如果button 重新覆写了样式,将会覆盖样式表中的样式,要想让样式表生效需要将重复的样式删除掉。这样才会显示样式:

    <Window.Resources>
        <Style x:Key="defaultButtonStyle" TargetType="Button">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="red"/>
            <Setter Property="Content" Value="hello"/>

        </Style>
    </Window.Resources>  
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource defaultButtonStyle}"  Content="press" Width="200" Height="40" />
            <Button Style="{StaticResource defaultButtonStyle}" FontSize="30" Foreground="Blue" Content="press"  />
            <Button Style="{StaticResource defaultButtonStyle}" Width="200" Height="40" />

        </StackPanel>
    </Grid>

样式的继承关系

样式之间也是可以继承的,需要使用到  BasedOn=""

如: BasedOn="{StaticResource baseButtonStyle}

    <Window.Resources>

        <Style  x:Key="baseButtonStyle" TargetType="Button">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="red"/>
        </Style>
        <Style x:Key="defaultButtonStyle" TargetType="Button" BasedOn="{StaticResource baseButtonStyle}">
            <Setter Property="Content" Value="hello"/>
        </Style>
    </Window.Resources>  
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource defaultButtonStyle}"  Content="press" Width="200" Height="40" />
            <Button Style="{StaticResource defaultButtonStyle}" FontSize="30" Foreground="Blue" Content="press"  />
            <Button Style="{StaticResource defaultButtonStyle}" Width="200" Height="40" />

        </StackPanel>
    </Grid>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值