当前位置: 首页 > news >正文

WPF 自定义控件库

一、使用场景

  • 开发自定义控件库时,向外部暴露可复用的样式、模板、画笔等资源。
  • 多模块应用中,共享通用资源(如主题样式)。
  • 需要避免资源键命名冲突的场景。

二 ,程序

image

 1.静态的后台代码 资源键

// MyControlLibrary/ResourceKeys.cs
using System.Windows;namespace MyControlLibrary;public static class ResourceKeys
{// 按钮样式资源键public static ComponentResourceKey CustomButtonStyleKey =>new ComponentResourceKey(typeof(ResourceKeys), "CustomButtonStyle");// 文本框样式资源键public static ComponentResourceKey CustomTextBoxStyleKey =>new ComponentResourceKey(typeof(ResourceKeys), "CustomTextBoxStyle");// 主题色画笔资源键public static ComponentResourceKey ThemeBrushKey =>new ComponentResourceKey(typeof(ResourceKeys), "ThemeBrush");
}

2.前台代码

<!-- MyControlLibrary/Themes/Generic.xaml -->
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:MyControlLibrary"><SolidColorBrush x:Key="{x:Static local:ResourceKeys.ThemeBrushKey}" Color="#FF2196F3"/><Style x:Key="{x:Static local:ResourceKeys.CustomButtonStyleKey}" TargetType="Button"><Setter Property="Background" Value="{StaticResource {x:Static local:ResourceKeys.ThemeBrushKey}}"/><Setter Property="Foreground" Value="White"/><Setter Property="Padding" Value="12,6"/><Setter Property="FontSize" Value="14"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Border Background="{TemplateBinding Background}" CornerRadius="4" Padding="{TemplateBinding Padding}"SnapsToDevicePixels="True"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="{x:Static local:ResourceKeys.CustomTextBoxStyleKey}" TargetType="TextBox"><Setter Property="BorderBrush" Value="{StaticResource {x:Static local:ResourceKeys.ThemeBrushKey}}"/><Setter Property="BorderThickness" Value="2"/><Setter Property="Padding" Value="8"/><Setter Property="FontSize" Value="14"/></Style>
</ResourceDictionary>

3.下面是引用上面的项目,在依赖项中引用一下

App.xaml

<Application x:Class="WpfApp.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><!-- 合并控件库的资源字典(Pack URI格式) --><ResourceDictionary Source="/MyControlLibrary;component/Themes/Generic.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

 

MainWindow.xaml

<!-- WpfApp/MainWindow.xaml -->
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:lib="clr-namespace:MyControlLibrary;assembly=MyControlLibrary"mc:Ignorable="d"Title=".NET 8 ComponentResourceKey 示例" Height="350" Width="500"><StackPanel Margin="20" ><TextBlock FontSize="16" FontWeight="Bold" Text="使用ComponentResourceKey的资源示例"/><!-- 使用自定义按钮样式 --><Button Content="自定义按钮" Style="{StaticResource {x:Static lib:ResourceKeys.CustomButtonStyleKey}}"/><!-- 使用自定义文本框样式 --><TextBox Text="自定义文本框" Style="{StaticResource {x:Static lib:ResourceKeys.CustomTextBoxStyleKey}}"/><!-- 使用主题色画笔 --><Border Height="60" Background="{StaticResource {x:Static lib:ResourceKeys.ThemeBrushKey}}"><TextBlock Text="主题色背景" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></StackPanel>
</Window>

 

http://www.jsqmd.com/news/23748/

相关文章:

  • 2025质量可靠的义乌刺绣工厂推荐榜
  • c# 使用 jwt
  • 2025义乌做刺绣的厂家推荐榜单
  • 2025 年液压旋转接头,高温蒸汽旋转接头,通水旋转接头厂家最新推荐,精准检测与稳定性能深度解析
  • Linux基本命令篇 —— date命令
  • 2025 年连铸机旋转接头,埋入式旋转接头,球体摆动旋转接头,造纸机旋转接头厂家最新推荐,精准检测与稳定性能深度解析
  • CorelDRAW的shell扩展ShellXP.dll导致资源管理器explorer.exe卡死/冻结/无响应/挂起
  • nef怎么转换成jpg格式你还不会?分享4个实用技巧
  • 2025 年非标旋转接头,异形旋转接头,钢铁厂旋转接头厂家最新推荐,聚焦高端定制需求与全案交付能力
  • 故障处理:ORA-02298: cannot validate (CTG.FK_CTG_LOGS_INT_201306) – parent keys not found
  • 2025年上海家电展会展台搭建服务商权威推荐榜单:展台搭建会展服务/会展展台装修设计/展台设计装修源头公司精选
  • 2025 年矿车生产,井下矿车,底侧卸式矿车厂家最新推荐,产能、专利、环保三维数据透视
  • macro出pin
  • 2025 年定制矿车,大型矿车,固定式矿车厂家最新推荐,产能、专利、环保三维数据透视
  • DP1312多协议高性能读卡芯片支持A/B/Felaca/18092智能门锁读卡器模拟卡兼容PN512 - 动能世纪
  • 2025 年矿用平板车,重型平板车,履带平板车,矿山平板车厂家最新推荐,产能、专利、环保三维数据透视!
  • 醒图电脑版下载与安装教程(2025最新版)
  • 2025 年 10 月翻斗式矿车,侧翻矿车,1 吨矿车,运输矿车厂家最新推荐,产能、专利、环保三维数据透视
  • 读书笔记:告别数据冗余!Oracle引用分区让父子表管理如此简单
  • 谷歌翻译 100 遍《我常常追忆过去》
  • 2025 年 10 月绕包电缆头,熔接电缆头,预制电缆头,冷缩管电缆头厂家最新推荐,产能、专利、环保三维数据透视
  • 2025 年江苏电缆附件,热缩电缆附件,冷缩电缆附件,预制电缆附件厂家最新推荐,产能、专利、环保三维数据透视
  • 9 GHz 巴伦做到 22 mm,HT-BAL-0006SMG 简单上机测一波
  • 2025年10月兰花油品牌推荐榜:五款精华油深度对比与选购指南
  • 低代码开发便捷的技术深度解析
  • PYTHON脚本验证端口访问权限(支持多种格式)
  • 2025年10月兰花油品牌推荐榜:五款高口碑产品深度对比解析
  • 杭州AI优化企业:国内GEO领域技术标杆 - 二当家
  • 构建定时 Agent,基于 Spring AI Alibaba 实现自主运行的人机协同智能 Agent
  • AI幻觉的真相:为什么ChatBI会“说谎”?