C# WPF之Material Design自定义颜色
C# WPF之Material Design自定义颜色
阅读导航
- 本文背景
- 代码实现
- 本文参考
1. 本文背景
主要介绍使用Material Design开源控件库的自定义颜色功能
2. 代码实现
使用 .Net Core 3.1 创建名为 “CustomColorDemo” 的WPF模板项目,添加两个个Nuget库:MaterialDesignThemes、MaterialDesignColors。
MaterialDesign控件库
2.1 引入MD控件样式
文件【App.xaml】
<Application x:Class="CustomColorDemo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> </ResourceDictionary.MergedDictionaries> <!--PRIMARY--> <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#349fda"/> <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#333333"/> <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#0288d1"/> <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/> <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#015f92"/> <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/> <!--ACCENT--> <SolidColorBrush x:Key="SecondaryAccentBrush" Color="#689f38"/> <SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#FFFFFF"/> </ResourceDictionary> </Application.Resources> </Application>
2.2 展示界面
文件【MainWindow.xaml】代码:
<Window x:Class="CustomColorDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignRaisedLightButton}" Width="90" Content="LIGHT" Margin="10"/> <Button Style="{StaticResource MaterialDesignRaisedButton}" Width="90" Content="MID" Margin="10"/> <Button Style="{StaticResource MaterialDesignRaisedDarkButton}" Width="90" Content="DARK" Margin="10"/> <Button Style="{StaticResource MaterialDesignRaisedAccentButton}" Width="90" Content="ACCENT" Margin="10"/> </StackPanel> <GroupBox Header="USING ACCENT" materialDesign:ColorZoneAssist.Mode="Accent"> <StackPanel Orientation="Horizontal"> <DatePicker Width="100" Margin="10"/> <CheckBox VerticalAlignment="Center" Content="Check Me" IsChecked="True" Margin="10"/> <ToggleButton Margin="10" VerticalAlignment="Center"/> </StackPanel> </GroupBox> <GroupBox Header="USING DARK" materialDesign:ColorZoneAssist.Mode="Dark"> <StackPanel Orientation="Horizontal"> <DatePicker Width="100" Margin="10"/> <CheckBox VerticalAlignment="Center" Content="Check Me" IsChecked="False" Margin="10"/> <ToggleButton IsChecked="True" Margin="10" VerticalAlignment="Center"/> </StackPanel> </GroupBox> </StackPanel> </Grid> </Window>
4个按钮使用MD控件4种样式(LIGHT、MID、DARK、ACCENT),附加属性 materialDesign:ColorZoneAssist.Mode 可以修改 GroupBox 的 Header 背景色,主要看 GroupBox 内的控件,CheckBox 与 ToggleButton 的外观已经修改。
3.参考
除非注明,文章均由 Dotnet9 整理发布,欢迎转载。
转载请注明本文地址:https://dotnet9.com/7187.html
欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章
时间如流水,只能流去不流回。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?