百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

基于 WPFDevelopers 库实现主题切换功能

yuyutoo 2025-04-08 20:27 12 浏览 0 评论

基于 WPFDevelopers 库实现主题切换功能

控件名:Theme

作 者:WPFDevelopersOrg - 驚鏵

原文链接[1]:https://github.com/WPFDevelopersOrg/WPFDevelopers

码云链接[2]:https://gitee.com/WPFDevelopersOrg/WPFDevelopers

  • 框架支持.NET4 至 .NET8
  • Visual Studio 2022;
1. 新增 Theme.cs
  • Theme 继承 ListBox,用于显示和颜色主题切换。
  • 预定义了种颜色(可自行添加其他色),来源于 WD早期的主题色
privatereadonly List themes = new List
{
new SolidColorBrush(Color.FromRgb(64, 158, 255)), // #409EFF
new SolidColorBrush(Color.FromRgb(255, 3, 62)), // #FF033E
new SolidColorBrush(Color.FromRgb(162, 27, 252)), // #A21BFC
new SolidColorBrush(Color.FromRgb(254, 148, 38)), // #FE9426
new SolidColorBrush(Color.FromRgb(0, 176, 80)), // #00B050
new SolidColorBrush(Color.FromRgb(255, 0, 127)) // #FF007F
};
  • Orientation 列表方向水平垂直
  • 监听 OnSelectionChanged 选择主题色时触发 ThemeManager
protectedoverridevoidOnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
if (SelectedItem == ) return;

var item = SelectedItem as ThemeItem;
if (item == ) return;

if (item.Background is SolidColorBrush solidColorBrush)
ThemeManager.Instance.SetColor(solidColorBrush.Color);
}
2. 新增 Theme.xaml
  • controls:ThemeItem主题色项。
  • wd:PathIcon 作为一个选中图标,当 IsSelected=True 则显示,反之则不显示。
  • Orientation 绑定 controls:Theme 控件的 Orientation 属性,支持水平垂直
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers">


<Style
x:Key="WD.ThemeItem"
BasedOn="{StaticResource WD.ControlBasicStyle}"
TargetType="{x:Type controls:ThemeItem}">

<SetterProperty="Width"Value="40" /> <SetterProperty="Height"Value="40" /> <SetterProperty="VerticalAlignment"Value="Center" /> <SetterProperty="HorizontalAlignment"Value="Stretch" /> <SetterProperty="VerticalContentAlignment"Value="Bottom" /> <SetterProperty="HorizontalContentAlignment"Value="Right" /> <SetterProperty="Padding"Value="0,0,4,4" /> <SetterProperty="Template"> <Setter.Value> <ControlTemplateTargetType="{x:Type controls:ThemeItem}"> <Border
x:Name="PART_Border"
Padding="2"
Background="Transparent"
BorderBrush="{TemplateBinding Background}"
BorderThickness="0"
CornerRadius="{Binding Path=(wd:ElementHelper.CornerRadius), RelativeSource={RelativeSource AncestorType=controls:Theme}}">
<wd:SmallPanel> <Border
x:Name="PART_BorderBackground"
Background="{TemplateBinding Background}"
CornerRadius="{Binding Path=(wd:ElementHelper.CornerRadius), RelativeSource={RelativeSource AncestorType=controls:Theme}}" />
<wd:PathIcon
Width="12"
Height="10"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Data="{StaticResource WD.CheckMarkGeometry}"
Foreground="{DynamicResource WD.BackgroundBrush}"
Kind="CheckMark"
Visibility="{TemplateBinding IsSelected,
Converter={StaticResource WD.Bool2VisibilityConverter}}"
/>
</wd:SmallPanel> </Border> <ControlTemplate.Triggers> <TriggerProperty="IsMouseOver"Value="True"> <SetterTargetName="PART_BorderBackground"Property="Opacity"Value=".8" /> <SetterTargetName="PART_Border"Property="BorderThickness"Value="1" /> </Trigger> <TriggerProperty="IsSelected"Value="True"> <SetterTargetName="PART_Border"Property="BorderThickness"Value="1" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter>
</Style>

<Style
x:Key="WD.Theme"
BasedOn="{StaticResource WD.ControlBasicStyle}"
TargetType="{x:Type controls:Theme}">

<SetterProperty="VerticalAlignment"Value="Center" /> <SetterProperty="HorizontalAlignment"Value="Left" /> <SetterProperty="Foreground"Value="{DynamicResource WD.PrimaryTextBrush}" /> <SetterProperty="ItemContainerStyle"Value="{StaticResource WD.ThemeItem}" /> <SetterProperty="Padding"Value="{StaticResource WD.Padding}" /> <SetterProperty="Template"> <Setter.Value> <ControlTemplateTargetType="{x:Type controls:Theme}"> <wd:WDBorder
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Path=(wd:ElementHelper.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}">
<ItemsPresenter
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Clip="{Binding RelativeSource={RelativeSource AncestorType=wd:WDBorder}, Path=ContentClip}" />
</wd:WDBorder> </ControlTemplate> </Setter.Value> </Setter> <SetterProperty="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <StackPanelwd:PanelHelper.Spacing="4"Orientation="{Binding Path=Orientation, RelativeSource={RelativeSource AncestorType=controls:Theme}}" /> </ItemsPanelTemplate> </Setter.Value> </Setter>
</Style>

<StyleBasedOn="{StaticResource WD.Theme}"TargetType="{x:Type controls:Theme}" />
</ResourceDictionary>
XAML 示例
  • 示例引入 WPFDevelopers 1.1.0.3-preview3 Nuget 正式包
  • 引入命名空间 xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
<controls:ThemeMargin="0,10" />

GitHub 源码地址[3]

Gitee 源码地址[4]

参考资料
[1]

原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers

[2]

码云链接: https://gitee.com/WPFDevelopersOrg/WPFDevelopers

[3]

GitHub 源码地址: https://github.com/WPFDevelopersOrg/WPFDevelopers/tree/dev/src/WPFDevelopers.Samples.Shared/Controls/Theme

[4]

Gitee 源码地址: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/tree/dev/src/WPFDevelopers.Samples.Shared/Controls/Theme

相关推荐

Python操作Word文档神器:python-docx库从入门到精通

Python操作Word文档神器:python-docx库从入门到精通动动小手,点击关注...

Python 函数调用从入门到精通:超详细定义解析与实战指南 附案例

一、函数基础:定义与调用的核心逻辑定义:函数是将重复或相关的代码块封装成可复用的单元,通过函数名和参数实现特定功能。它是Python模块化编程的基础,能提高代码复用性和可读性。定义语法:...

等这么长时间Python背记手册终于来了,入门到精通(视频400集)

本文毫无套路!真诚分享!前言:无论是学习任何一门语言,基础知识一定要扎实,基础功非常的重要,找一个有丰富编程经验的老师或者师兄带着你会少走很多弯路,你的进步速度也会快很多,无论我们学习的目的是什么,...

图解Python编程:从入门到精通系列教程(附全套速查表)

引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...

Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够

本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题。主要内容有:数字、字符串和元组,语句与语法,函数定义,列表、集、字典,用户输入和输出等内置数据结构,类和对象,...

Python函数全解析:从入门到精通,一文搞定!

1.为什么要用函数?函数的作用:封装代码,提高复用性,减少重复,提高可读性。...

Python中的单例模式:从入门到精通

Python中的单例模式:从入门到精通引言单例模式是一种常用的软件设计模式,它保证了一个类只有一个实例,并提供一个全局访问点。这种模式通常用于那些需要频繁创建和销毁的对象,比如日志对象、线程池、缓存等...

【Python王者归来】手把手教你,Python从入门到精通!

用800个程序实例、5万行代码手把手教你,Python从入门到精通!...

Python从零基础入门到精通:一个月就够了

如果想从零基础到入门,能够全职学习(自学),那么一个月足够了。...

Python 从入门到精通:一个月就够了

要知道,一个月是一段很长的时间。如果每天坚持用6-7小时来做一件事,你会有意想不到的收获。作为初学者,第一个月的月目标应该是这样的:熟悉基本概念(变量,条件,列表,循环,函数)练习超过30个编...

Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!

Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...

神仙级python入门教程(非常详细),从0到精通,从看这篇开始!

python入门虽然简单,很多新手依然卡在基础安装阶段,大部分教程对一些基础内容都是一带而过,好多新手朋友,对一些基础知识常常一知半解,需要在网上查询很久。...

Python类从入门到精通,一篇就够!

一、Python类是什么?大家在生活中应该都见过汽车吧,每一辆真实存在、能在路上跑的汽车,都可以看作是一个“对象”。那这些汽车是怎么生产出来的呢?其实,在生产之前,汽车公司都会先设计一个详细的蓝图...

学习Python从入门到精通:30天足够了,这才是python基础的天花板

当年2w买的全套python教程用不着了,现在送给有缘人,不要钱,一个月教你从入门到精通1、本套视频共487集,本套视频共分4季...

30天Python 入门到精通(3天学会python)

以下是一个为期30天的Python入门到精通学习课程,专为零基础新手设计。课程从基础语法开始,逐步深入到面向对象编程、数据处理,最后实现运行简单的大语言模型(如基于HuggingFace...

取消回复欢迎 发表评论: