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

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

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

相关推荐

VBA中利用Instr函数(vba int函数)

【分享成果,随喜正能量】每一个在你的生命里出现的人,都有原因,喜欢你的人给了你温暖和勇气,你喜欢的人让你学会了爱和自持,你不喜欢的人教会你宽容与尊重,不喜欢你的人让你自省与成长。。...

Insta360 Link体验:支持4K画质,一款使用场景丰富的AI云台摄像头

记者|王公逸伴随直播、线上会议需求的兴起,网络直播的需求愈发增大,8月2日,影石Insta360正式推出全新产品:Insta360Link,这是一款AI智能云台摄像头。从产品形态来说,Insta3...

VBA技术资料MF299:利用Instr进行文本查找

我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。“VBA语言専攻”提供的教程一共九套,分为初级、中级、高级三大部分,教程是对VB...

Fabric.js 拖放元素进画布 - 掘金

本文简介点赞+关注+收藏=学会了学习Fabric.js,我的建议是看文档不如看demo。本文实现的功能:将元素拖进到画布中并生成对应的图形或图片。效果如下图所示:...

Vue3为什么推荐使用ref而不是reactive

为什么推荐使用ref而不是reactivereactive本身具有很大局限性导致使用过程需要额外注意,如果忽视这些问题将对开发造成不小的麻烦;ref更像是vue2时代optionapi的data的替...

Fabric.js 样式不更新怎么办?(js更改样式)

本文简介带尬猴,我嗨德育处主任不知道你有没有遇到过在使用Fabric.js时无意中一些骚操作修改了元素的样式,但刷新画布却没更新元素样式?如果你也遇到同样的问题的话,可以尝试使用本文的方法。...

Fabric.js 修改画布交互方式到底有什么用?

本文简介点赞+关注+收藏=学会了fabric.js为我们提供了很多厉害的方法。今天要搞明白的一个东西是canvas.interactive。官方文档对canvas.interact...

Rust Web编程:第五章 在浏览器上显示内容

我们现在正处于可以构建一个Web应用程序的阶段,该应用程序可以使用不同的方法和数据管理一系列HTTP请求。这很有用,特别是当我们为微服务构建服务器时。然而,我们也希望非程序员能够与我们的应...

Fabric.js 自由绘制椭圆 - 掘金(canvas画椭圆)

本文简介点赞+关注+收藏=学会了本文讲解在Fabric.js中如何自由绘制椭圆形,如果你还不了解Fabric.js,可以查阅《Fabric.js从入门到精通》。效果如下图所示...

手把手教你实现JS手搓&quot;防抖&quot;优化代码——专业的事用专业的方法!

前言在我们前端编程中,假如我们要给后端发送请求,万一手抖多点了几次,多发送了几遍怎么办?解决方案:防抖!这种事就要交给我们专业的“防抖”先生来处理!今天,我们就来教大家手搓“防抖”...

详解虚拟DOM与Diff算法(虚拟dom一定比实际dom快吗)

vue的虚拟DOM,Diff算法,其中一些关键的地方从别处搬运了一些图进行说明(感谢制图的大佬),也包含比较详细的源码解读。...

走进 React Fiber 的世界(我走进你的世界手势舞视频)

文/阿里淘系F(x)Team-冷卉Fiber设计思想Fiber是对React核心算法的重构,facebook团队使用两年多的时间去重构React的核心算法,在React16以上...

前端新一代框架 Svelte 火了!十个场景带你简单认识它!

近几年听到的主流框架都是Vue、React、Angular,但其实有一个框架在国外非常火,用起来也是很方便,那就是...

借助DeepSeek实现了一个PDF阅读器

1、简介使用pdf.js库加载和显示PDF文件。实现了翻页、缩放功能。提供了基本的错误处理。功能特点:支持选择本地PDF文件。可以逐页查看PDF内容。支持放大缩小功能。界面简洁,易于使...

DeepSeek代码之旅1:卫星地图标记方法之——html语言的实现

最近遇到一个任务,具体功能如下:1、调用高德地图API,图层为卫星图层,根据需要标记兴趣点;2、标记完成后可以保存兴趣点,便于下次加载历史兴趣点。...

取消回复欢迎 发表评论: