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

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

yuyutoo 2025-04-08 20:27 6 浏览 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

相关推荐

Mysql和Oracle实现序列自增(oracle创建序列的sql)

Mysql和Oracle实现序列自增/*ORACLE设置自增序列oracle本身不支持如mysql的AUTO_INCREMENT自增方式,我们可以用序列加触发器的形式实现,假如有一个表T_WORKM...

关于Oracle数据库12c 新特性总结(oracle数据库19c与12c)

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

MySQL CREATE TABLE 简单设计模板交流

推荐用MySQL8.0(2018/4/19发布,开发者说同比5.7快2倍)或同类型以上版本....

mysql学习9:创建数据库(mysql5.5创建数据库)

前言:我也是在学习过程中,不对的地方请谅解showdatabases;#查看数据库表createdatabasename...

MySQL面试题-CREATE TABLE AS 与CREATE TABLE LIKE的区别

执行"CREATETABLE新表ASSELECT*FROM原表;"后,新表与原表的字段一致,但主键、索引不会复制到新表,会把原表的表记录复制到新表。...

Nike Dunk High Volt 和 Bright Spruce 预计将于 12 月推出

在街上看到的PandaDunk的超载可能让一些球鞋迷们望而却步,但Dunk的浪潮仍然强劲,看不到尽头。我们看到的很多版本都是为女性和儿童制作的,这种新配色为后者引入了一种令人耳目一新的新选择,而...

美国多功能舰载雷达及美国海军舰载多功能雷达系统技术介绍

多功能雷达AN/SPY-1的特性和技术能力,该雷达已经在美国海军服役了30多年,其修改-AN/SPY-1A、AN/SPY-1B(V)、AN/SPY-1D、AN/SPY-1D(V),以及雷神...

汽车音响怎么玩,安装技术知识(汽车音响怎么玩,安装技术知识视频)

全面分析汽车音响使用或安装技术常识一:主机是大多数人最熟习的音响器材,有关主机的各种性能及规格,也是耳熟能详的事,以下是一些在使用或安装时,比较需要注意的事项:LOUDNESS:几年前的主机,此按...

【推荐】ProAc Response系列扬声器逐个看

有考牌(公认好声音)扬声器之称ProAcTablette小音箱,相信不少音响发烧友都曾经,或者现在依然持有,正当大家逐渐掌握Tablette的摆位设定与器材配搭之后,下一步就会考虑升级至表现更全...

#本站首晒# 漂洋过海来看你 — BLACK&amp;DECKER 百得 BDH2000L无绳吸尘器 开箱

作者:初吻给了烟sco混迹张大妈时日不短了,手没少剁。家里有了汪星人,吸尘器使用频率相当高,偶尔零星打扫用卧式的实在麻烦(汪星人:你这分明是找借口,我掉毛是满屋子都有,铲屎君都是用卧式满屋子吸的,你...

专题|一个品牌一件产品(英国篇)之Quested(罗杰之声)

Quested(罗杰之声)代表产品:Q212FS品牌介绍Quested(罗杰之声)是录音监听领域的传奇品牌,由英国录音师RogerQuested于1985年创立。在成立Quested之前,Roger...

常用半导体中英对照表(建议收藏)(半导体英文术语)

作为一个源自国外的技术,半导体产业涉及许多英文术语。加之从业者很多都有海外经历或习惯于用英文表达相关技术和工艺节点,这就导致许多英文术语翻译成中文后,仍有不少人照应不上或不知如何翻译。为此,我们整理了...

Fyne Audio F502SP 2.5音路低音反射式落地音箱评测

FyneAudio的F500系列,有新成员了!不过,新成员不是新的款式,却是根据原有款式提出特别版。特别版产品在原有型号后标注了SP字样,意思是SpecialProduction。Fyne一共推出...

有哪些免费的内存数据库(In-Memory Database)

以下是一些常见的免费的内存数据库:1.Redis:Redis是一个开源的内存数据库,它支持多种数据结构,如字符串、哈希表、列表、集合和有序集合。Redis提供了快速的读写操作,并且支持持久化数据到磁...

RazorSQL Mac版(SQL数据库查询工具)

RazorSQLMac特别版是一款看似简单实则功能非常出色的SQL数据库查询、编辑、浏览和管理工具。RazorSQLformac特别版可以帮你管理多个数据库,支持主流的30多种数据库,包括Ca...

取消回复欢迎 发表评论: