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

相关文章:

  • 2025质量可靠的义乌刺绣工厂推荐榜
  • DP1312多协议高性能读卡芯片支持A/B/Felaca/18092智能门锁读卡器模拟卡兼容PN512 - 动能世纪
  • 2025年10月兰花油品牌推荐榜单:多维度深度对比与选择指南
  • 2025 年1KV 冷缩硅橡胶电缆附件,冷热缩电缆附件,绕包电缆附件,熔接电缆附件厂家最新推荐,产能、专利、环保三维数据透视
  • 低代码开发便捷的技术深度解析
  • 2025年浅拾兰花双萃致臻精华油:从成分与科技维度解析其护肤功效
  • 销售公司绩效考核全攻略:维度、原则与数字化赋能方案
  • 题解:P4434 [COCI 2017/2018 #2] ​​Usmjeri
  • 小程序-跳转到公众号
  • 如何解决一堆向量的问题?10、Self-attention - -一叶知秋
  • 洞悉过往,一目了然:浅述视频融合平台EasyCVR如何实现海量视频录像的智能检索与高效回看
  • 2025年国内外五款AI编程工具深入对比与推荐排行
  • CSPS 前后的话
  • 2025 年 10 月云仓 ERP,云仓 saas 系统,云仓代发系统公司最新推荐,技术实力与市场口碑深度解析
  • iOS混淆实战用多工具组合把IPA加固做成可复用的工程能力(iOS混淆 IPA加固 无源码混淆
  • cyclonessd ROS2 lidar topic 数据丢帧 系统配置
  • 2025 年 10 月 WMS 系统,WMS 软件,wms 仓储管理系统公司最新推荐,聚焦资质、案例、售后的优质机构深度解读
  • Go语言测试全攻略:从单元测试到模糊测试
  • 2025 年 10 月进销存 erp,供应链 erp,零售 ERP 公司最新推荐,聚焦资质、案例、售后的五家机构深度解读!