【C# WPF】Style全局样式和资源字典

1.全局样式:

在Window.Resource中声明一个样式,总体为白色,为了更有区分度,采用BasedOn这一继承方式来在保留字体和边缘设置的基础上,更改颜色。

<Window x:Class="WpfApp1.Window1"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Day2" Height="450" Width="800">

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="White"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Margin" Value="10"/>
        </Style>

        <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Pink"/>
        </Style>

    </Window.Resources>
    
    <StackPanel>
        <Button Content="登录" Style="{StaticResource LoginStyle}"/>
        <Button Content="退出" Style="{StaticResource QuitStyle}"/>
    </StackPanel>
        
</Window>

窗体结构:
在这里插入图片描述

2.资源字典

第一种方法,仅能将样式应用在本窗体中,不符合封装、多用的思想。
所以要将Style资源样式分离出来,静态调用。

在项目上,右击添加–资源字典,在资源字典中添加Style类型:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="Background" Value="White"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Margin" Value="10"/>
    </Style>

    <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Red"/>
    </Style>
    <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Pink"/>
    </Style>
    
</ResourceDictionary>

这样就将资源样式添加到了资源字典中。

下面是如何调用这些写好的样式,找到App.xaml,在<Application.Resources>中添加:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="WpfApp1;component/BaseButtonStyle.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

这样就被全局包含了。

测试一下,跟之前是一样的,并且在其他Window中也能调用。
第一个窗口:

    <StackPanel>
        <Button Content="登录" Style="{StaticResource LoginStyle}"/>
        <Button Content="退出" Style="{StaticResource QuitStyle}"/>
    </StackPanel>

其他新建窗口:

    <Grid>
        <Button Style="{StaticResource LoginStyle}"/>
    </Grid>
  • 18
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值