wfp学习-style

文章介绍了WPF中Style的定义和使用方法,强调了Style的TargetType以及BasedOn属性在样式继承中的作用。通过示例展示了如何定义基础样式baseBtn,然后创建btnStyle1进行继承并覆盖部分属性。同时,文中提到了控件直接设置的属性会具有更高的优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

wpf的style和web端的差不多。都是上面定义,下面使用。等级越低,优先级越高
我是在这学的:
https://www.bilibili.com/video/BV1nY411a7T8/?p=2&vd_source=b6991929b1d37895e965acaa56537632

<Window.Resources>
        <Style x:Key="baseBtn" TargetType="Button">
            <!-- TargetType:这个style是给哪个控件的,button还是checkbox -->
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Background" Value="Red"/>
            <Setter Property="FontFamily" Value="正楷"/>
        </Style>

        <Style x:Key="btnStyle1" TargetType="Button"  BasedOn="{StaticResource baseBtn}" >
            <!--BasedOn:继承同类型的style 如果有重复的元素,本级设置的元素值  优先级最高-->
            <Setter Property="FontSize" Value="14"/>

        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Horizontal">
            <Button Height="120" Width="120" Style="{StaticResource btnStyle1}"  Content="我是btn1" />
            <!--Style:继承-Window.Resources的 Style>-->


            <Button Height="120" Width="120"  Content="我是btn2" Style="{StaticResource baseBtn}" />
            
            
            <Button Height="120" Width="120" FontSize="20"  Background="Aquamarine"  Content="我是btn2" Style="{StaticResource btnStyle1}" />
            <!--直接在控件里定义Background的值,优先使用控件的值。等级余额低,优先级越高-->


            <CheckBox Height="120" Width="156" FontSize="21" Background="Red" Content="CheckBox" Margin="0,150,0,149"/>
        </StackPanel>

    </Grid>

------------------------------------↓↓↓↓效果图↓↓↓↓----------------------------------------------
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值