ARTICLE · 1530594
一套可复用的 WPF 上位机 UI 模板(SCADA-HMI 风格)
前言
做工业上位机软件,界面这块挺让人头疼的。每个项目都得重新画一遍按钮、调一遍颜色、对齐一遍布局,做着做着就走样了——这个页面按钮在左边,下个页面跑到右边;这里圆角 8,那边圆角 12,时间长了项目里到处都是不一致的 UI。
作者做了一个 SCADA 类的上位机项目,趁这个机会把 UI 层面的东西统一整理了一遍。从颜色、字体、圆角、间距这些最基础的视觉规范,到 Button、Card、DataGrid、ListBox 这些控件的全局样式,再到页面布局的几种固定模式,全部收敛到一套主题里。这篇文章就是这套 UI 规范的完整记录,给团队内部做新页面用,也顺手分享出来。
项目介绍
一个工业上位机 / SCADA-HMI 风格的项目 UI 模板,基于 WPF + Prism + CommunityToolkit.MVVM 构建。整体采用浅色扁平卡片化设计,灰蓝色页面底搭配白色圆角卡片分区,蓝色为主色体系。
项目把 UI 层的样式、控件、页面布局全部收敛到 Themes 目录下,新页面只需按照规范组装 XAML,就能保持和已有页面完全一致的视觉风格。同时包含了登录门禁、主窗口壳、导航菜单和六个典型业务页面的完整实现。
项目功能
项目特点
主题集中管理:9 个 ResourceDictionary 在 App.xaml 统一合并,换肤或调整配色只需改 Colors.xaml
控件全局默认样式:ComboBox、DataGrid、ListBox、ScrollBar 使用隐式 Style,页面无需显式指定即可保持一致
页面布局语法统一:标题 + ContentCardStyle 分区 + 右侧 IconButton 工具栏,六种页面模式覆盖绝大多数业务场景
图标与颜色解耦:Icons.xaml 仅存 Geometry,图标颜色跟随按钮 Foreground,没有硬编码色值
MVVM 包规范约束:使用 CommunityToolkit.Mvvm 的 ObservableProperty 和 RelayCommand,View 只负责布局和绑定
项目技术
项目代码
资源字典合并(App.xaml)
所有样式在 App.xaml 中统一合并,加载顺序有讲究——Colors 必须最先,Buttons 依赖 Controls 命名空间,DataGrid 又依赖 ComboBox 的 ItemStyle。
<ResourceDictionary.MergedDictionaries><ResourceDictionarySource="Themes/Colors.xaml" /><ResourceDictionarySource="Themes/Icons.xaml" /><ResourceDictionarySource="Themes/Buttons.xaml" /><ResourceDictionarySource="Themes/Cards.xaml" /><ResourceDictionarySource="Themes/ComboBox.xaml" /><ResourceDictionarySource="Themes/TextBox.xaml" /><ResourceDictionarySource="Themes/DatePicker.xaml" /><ResourceDictionarySource="Themes/DataGrid.xaml" /><ResourceDictionarySource="Themes/ListBox.xaml" /><ResourceDictionarySource="Themes/ScrollViewer.xaml" /></ResourceDictionary.MergedDictionaries>主窗口结构(MainWindow.xaml)
主窗口采用无边框设计,通过 WindowChrome 实现自定义标题栏和窗口缩放。
<Windowx:Class="WPF_UITemplate.Views.MainWindow"xmlns:controls="clr-namespace:WPF_UITemplate.Controls"WindowStyle="None"AllowsTransparency="False"ResizeMode="CanResize"Width="1600"Height="900"><WindowChrome.WindowChrome><WindowChromeCaptionHeight="0"ResizeBorderThickness="6" /></WindowChrome.WindowChrome><GridBackground="{StaticResource PageBackgroundBrush}"><Grid.RowDefinitions><RowDefinitionHeight="50" /><RowDefinitionHeight="*" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinitionWidth="180" /><ColumnDefinitionWidth="*" /></Grid.ColumnDefinitions><!-- TopBar --><controls:TopBarGrid.Row="0"Grid.ColumnSpan="2" /><!-- SideMenu --><controls:SideMenuGrid.Row="1"Grid.Column="0"DataContext="{Binding}" /><!-- 内容区 --><BorderGrid.Row="1"Grid.Column="1"Padding="20"BorderBrush="{StaticResource BorderBrush}"BorderThickness="1,0,0,0"><ContentControlContent="{Binding CurrentView}" /></Border></Grid></Window>IconButton 自定义控件
系统按钮统一使用 IconButton 控件,IconData 绑定 SVG Geometry,文字和图标在同一按钮内。
publicclassIconButton : Button{publicstaticreadonly DependencyProperty IconDataProperty = DependencyProperty.Register(nameof(IconData), typeof(Geometry), typeof(IconButton));publicstaticreadonly DependencyProperty IconSizeProperty = DependencyProperty.Register(nameof(IconSize), typeof(double), typeof(IconButton), new PropertyMetadata(14.0));publicstaticreadonly DependencyProperty IconMarginProperty = DependencyProperty.Register(nameof(IconMargin), typeof(Thickness), typeof(IconButton), new PropertyMetadata(new Thickness(0, 0, 6, 0)));public Geometry? IconData { get => (Geometry?)GetValue(IconDataProperty); set => SetValue(IconDataProperty, value); }publicdouble IconSize { get => (double)GetValue(IconSizeProperty); set => SetValue(IconSizeProperty, value); }public Thickness IconMargin { get => (Thickness)GetValue(IconMarginProperty); set => SetValue(IconMarginProperty, value); }}XAML 中的用法:
<controls:IconButtonContent="查询"MinWidth="86"IconData="{StaticResource IconSearch}"Style="{StaticResource PrimaryIconButtonStyle}"Command="{Binding SearchCommand}" />ContentCardStyle(卡片容器)
所有功能分区都用统一的卡片样式,白底、12 圆角、细边框。
<Stylex:Key="ContentCardStyle"TargetType="Border"><SetterProperty="Background"Value="{StaticResource CardBackgroundBrush}" /><SetterProperty="CornerRadius"Value="12" /><SetterProperty="Padding"Value="10" /><SetterProperty="BorderBrush"Value="{StaticResource BorderBrush}" /><SetterProperty="BorderThickness"Value="1" /></Style>页面中的标准分区卡:
<BorderStyle="{StaticResource ContentCardStyle}"><StackPanel><TextBlockFontSize="18"FontWeight="SemiBold"Foreground="{StaticResource TextPrimaryBrush}"Text="卡片标题" /><TextBlockMargin="0,6,0,10"FontSize="13"Foreground="{StaticResource TextSecondaryBrush}"Text="卡片说明" /><!-- 卡片内容 --></StackPanel></Border>DataGrid 隐式样式
合并 DataGrid.xaml 后,所有 DataGrid 自动应用全局样式,行高 30px、表头 32px、隔行变色、悬停高亮,无需在每个页面重复配置。
<Stylex:Key="IndustrialDataGridStyle"TargetType="DataGrid"><SetterProperty="RowHeight"Value="30" /><SetterProperty="MinRowHeight"Value="30" /><SetterProperty="HeadersVisibility"Value="Column" /><SetterProperty="GridLinesVisibility"Value="All" /><SetterProperty="AlternationCount"Value="2" /><SetterProperty="SelectionMode"Value="Single" /><SetterProperty="SelectionUnit"Value="FullRow" /><SetterProperty="CanUserAddRows"Value="False" /><SetterProperty="AutoGenerateColumns"Value="False" /><SetterProperty="BorderBrush"Value="{StaticResource IndustrialDataGridBorderBrush}" /><SetterProperty="BorderThickness"Value="1" /></Style>只读列表的列定义示例:
<DataGridTextColumnHeader="采集时间"Width="180"Binding="{Binding CollectTime, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}"ElementStyle="{StaticResource IndustrialDataGridCenteredTextStyle}" />应用启动流程(App.xaml.cs)
启动时先做单实例检测,再弹登录窗,登录成功后才显示主窗口。
protectedoverridevoidOnStartup(StartupEventArgs e){base.OnStartup(e);// 单实例检测 _mutex = new Mutex(true, "WPF_UITemplate_SingleInstance", outbool isFirstInstance);if (!isFirstInstance) { MessageBox.Show("程序已在运行中", "提示", MessageBoxButton.OK, MessageBoxImage.Information); Shutdown(0);return; }// 构建 DI 容器var services = new ServiceCollection(); ConfigureServices(services); Services = services.BuildServiceProvider();// 登录门禁var loginWindow = Services.GetRequiredService<LoginWindow>();bool? loginResult = loginWindow.ShowDialog();if (loginResult != true) { Shutdown(0);return; }// 启动主窗口var mainWindow = Services.GetRequiredService<MainWindow>(); mainWindow.Show();}MVVM 模式示例(HistoryDataViewModel)
使用 CommunityToolkit.Mvvm 源码生成功能,减少样板代码。
publicpartialclassHistoryDataViewModel : ObservableObject{ [ObservableProperty]privatestring barcodeKeyword = string.Empty; [ObservableProperty]private DateTime queryStartDate = DateTime.Today.AddDays(-7); [ObservableProperty]private DateTime queryEndDate = DateTime.Today; [RelayCommand]privateasync Task SearchAsync() {// 执行查询逻辑 } [RelayCommand(CanExecute = nameof(CanExport))]privateasync Task ExportCsvAsync() {// 导出 CSV }privateboolCanExport() => Records.Count > 0;}项目效果
主界面包含六个典型页面,覆盖了上位机软件的常见业务场景:
登录页:480×560 居中弹窗,卡片内包含 Logo、标题、表单、错误提示和登录按钮

首页:左栏为生产状态卡、当前条码、PLC 状态灯网格,右栏为运行日志面板,状态灯用 UniformGrid 四列布局

历史数据:查询条件卡 + 主表 DataGrid + 明细从表 DataGrid(绑定主表选中项)

配方参数:可编辑 DataGrid(单元格内嵌 ComboBox)+ 分页控件 + 状态消息

参数配置:双列 Grid 布局,PLC 参数卡 + 连接状态卡,右侧通讯测试读写分区

手动录入弹窗:720×560 独立窗口,带 DataGrid 预览和确认保存按钮
所有页面的按钮、卡片、表格、输入框样式完全一致,组件之间的间距、圆角、颜色统一,视觉上整体感很强。
总结
做 UI 规范这事,难的不是写样式,是让团队里每个人在做新页面的时候都能用上统一的样式,而不是自己又重新写一套。这套模板的做法是:
1、样式集中管理:所有颜色、字体、圆角、间距都在 Themes 里,页面 XAML 只引用资源 Key,不许硬编码色值。
2、隐式样式覆盖大多数场景:ComboBox、DataGrid、ListBox、ScrollBar 全部全局默认,页面里直接写控件名就能用上正确的样式。
3、页面布局模式固化:六种页面模板覆盖了列表查询、参数配置、首页总览、弹窗编辑等常见场景,新页面按模板套即可。
4、MVVM 约束明确:View 只做布局和绑定,业务逻辑在 ViewModel,用 ObservableProperty 和 RelayCommand 减少模板代码。
这套规范已经在项目里跑通了,后续有新页面照着写就行,不会再出现"这个按钮怎么比旁边那个高 2 像素"这种问题了。
关键词
#WPF、UI 设计规范、#工业上位机、#SCADA、#HMI 风格、MVVM、Prism 框架、CommunityToolkit.Mvvm、DataGrid 全局样式、#自定义控件、资源字典、浅色主题、卡片式布局、XAML














































































