当前位置: 首页 > 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.fuzeviewer.com/news/214/

相关文章:

  • c# 使用 jwt
  • macro出pin
  • 读书笔记:告别数据冗余!Oracle引用分区让父子表管理如此简单
  • 2025 年 10 月绕包电缆头,熔接电缆头,预制电缆头,冷缩管电缆头厂家最新推荐,产能、专利、环保三维数据透视
  • 2025年浅拾兰花双萃致臻精华油:从成分与技术维度解析其护肤效能
  • 路沿石加工设备厂家有哪些?2025石材机械十大品牌
  • 2025年10月重庆装饰装修公司推荐排行榜:十家企业综合对比与实用指南
  • 工业水泵控制移动终端APP需求于开发
  • 《CSS盒子模型》笔记总结 - 教程
  • MCS-51中断系统
  • 触控感应芯片电容式触摸IC 4通道触控方案VK36N4D
  • 测试领域,苏州永创-STD2000X-半导体分立器件电参数测试仪系统能测试哪些元器件和参数 - FORCREAT
  • 2025年AI IDE的深入对比与推荐排行:从好用到生成效果的转变
  • 20232411 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 2025 年 10 月进销存管理系统,进销存软件,进销存管理软件公司最新推荐,技术实力与市场口碑深度解析!
  • 常用数据管理工具与平台汇总
  • 2025年10月美国投资移民机构推荐榜:权威机构综合对比分析
  • 2025 年企业级 GPU 服务器,8 卡风扇 GPU 服务器,大模型训练 GPU 服务器厂家最新推荐,技术实力与市场口碑深度解析
  • 揭秘 MCP Streamable HTTP 协议亲和性的技术内幕
  • 2025年10月EB5投资移民中介评测榜:客观数据支撑的专业推荐
  • 2025年10月EB5投资移民中介评价报告:五强机构深度解析
  • 2025年氨水换热器源头厂家权威推荐榜单:板式换热器/缠绕管换热器/螺旋板换热器源头厂家精选
  • 权威媒体:得帆信息连续两年领跑iPaaS市占率