wpf之样式

前言

wpf的style样式用于将一组属性设置封装起来放置于资源文件中,从而轻松地应用到多个元素上,从而实现 UI 的统一和换肤功能,style主要有Setter、Trigger两个特性,

1、Setter

下面的代码中使用style通过Setter封装了三个属性Background、Foreground、FontSize,让这三个属性成为一个样式,并且通过TargetType="Button"来指定Button控件使用这个样式,并且使用x:Key="MyButtonStyle"来为这个样式起了一个名称,并且这个样式放置于Window.Resources中使样式成为了资源文件。最后使用Button时,通过Style="{StaticResource MyButtonStyle}"来让Button使用指定的样式.

csharp 复制代码
<Window x:Class="wpf之Style.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:wpf之Style"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Red" />
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="25"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel >
            <Button  Height=" 100" Style="{StaticResource MyButtonStyle}"  Content="样式Button"/>
            <Button  Height=" 100" Content="普通Button" />
        </StackPanel >
    </Grid>
</Window>

2、运行结果

从运行结果可以看出,使用了样式的Button,背景色、字体颜色、字体大小和不使用样式的Button比都得到了改变。

2、Trigger

Trigger称为触发器,Property是触发器关注的属性,Value是触发的条件,下面的代码中使用了两个触发器,当CheckBox的IsChecked为true时,背景色设置为Green,否则设置为红色

csharp 复制代码
<Window x:Class="wpf之Style.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:wpf之Style"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="MyCheckBoxStyle" TargetType="CheckBox" >
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True" >
                    <Setter Property="Background" Value="Green"    />
                </Trigger>
                <Trigger Property="IsChecked" Value="False"  >
                    <Setter Property="Background" Value="Red"  />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical" >
            <CheckBox   Style="{StaticResource MyCheckBoxStyle}"  Content="样式CheckBox"/>
            <CheckBox   Content="普通CheckBox" />
        </StackPanel >
    </Grid>
</Window>

1)IsChecked为true

2)IsChecked为false

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》

2、《C#与PLC通信从入门到精通 》

3、《C# Modbus通信从入门到精通》

4、《C#Socket通信从入门到精通 》

5、《C# MES通信从入门到精通》

6、《winform控件从入门到精通》

7、《C#操作MySql数据库从入门到精通》

相关推荐
Xin_ye1008612 小时前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
rockey62712 小时前
AScript异步执行与await关键字
c#·.net·script·eval·expression·异步执行·动态脚本
程序leo源14 小时前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
枫叶林FYL14 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
月巴月巴白勺合鸟月半18 小时前
质本洁来还洁去,强于污淖陷文本
c#
Xin_ye1008619 小时前
C# 零基础到精通教程 - 第八章:面向对象编程(进阶)——继承与多态
开发语言·c#
她说彩礼65万19 小时前
WPF 多值转换器
wpf
asdzx6720 小时前
使用 C# 打印 Excel 文档(详细教程)
c#·excel
伽蓝_游戏1 天前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
2501_930707781 天前
使用C#代码拆分 PowerPoint 演示文稿
开发语言·c#·powerpoint