WPF基础入门--样式的继承及使用

-----------------------------------------

需要加微信群

52209ab9043d1765d4e60fdcbf30c743.png

的加我微信z438679770,我邀请进群,切记备注 :进群

---------------------------------------

WPF的样式需要显示声明继承,即使用Style的BasedOn属性。

我们先在资源中定义一个基样式:

<Style x:Key="baseButtonStyle"  TargetType="Button">
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="Background" Value="Gray"/>
</Style>

然后我们定义两个继承自它的样式,分别为对应按钮baseButtonStyle1和baseButtonStyle2的样式:

<Style x:Key="baseButtonStyle1"  TargetType="Button" BasedOn="{StaticResource baseButtonStyle}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="40"/>
</Style>
        <Style x:Key="baseButtonStyle2"  TargetType="Button" BasedOn="{StaticResource baseButtonStyle1}">
            <Setter Property="Width" Value="120"/>
            <Setter Property="Height" Value="50"/>
</Style>

BasedOn属性设为了我们先前设置的“baseButtonStyle”,各自追加了一些属性的设置。

现在在界面上添加三个按钮控件:

<Grid>
        <StackPanel>
            <Button Style="{StaticResource baseButtonStyle}" Content="hello"/>
            <Button Style="{StaticResource baseButtonStyle1}" Content="hello"/>
            <Button Style="{StaticResource baseButtonStyle2}" Content="hello"/>
        </StackPanel>
    </Grid>

现在界面效果如下:

84e3bde6a78f82d122daa20314589b78.png

可以看到,按钮继承了基样式中的背景和字体和字体颜色。

全部xaml如下:

<Window x:Class="WpfApp9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp9"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="baseButtonStyle"  TargetType="Button">
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="Background" Value="Gray"/>
</Style>
        <Style x:Key="baseButtonStyle1"  TargetType="Button" BasedOn="{StaticResource baseButtonStyle}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="40"/>
</Style>
        <Style x:Key="baseButtonStyle2"  TargetType="Button" BasedOn="{StaticResource baseButtonStyle1}">
            <Setter Property="Width" Value="120"/>
            <Setter Property="Height" Value="50"/>
</Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource baseButtonStyle}" Content="hello"/>
            <Button Style="{StaticResource baseButtonStyle1}" Content="hello"/>
            <Button Style="{StaticResource baseButtonStyle2}" Content="hello"/>
        </StackPanel>
    </Grid>
</Window>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zls365365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值