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

相关文章:

  • CorelDRAW的shell扩展ShellXP.dll导致资源管理器explorer.exe卡死/冻结/无响应/挂起
  • 2025 年定制矿车,大型矿车,固定式矿车厂家最新推荐,产能、专利、环保三维数据透视
  • 醒图电脑版下载与安装教程(2025最新版)
  • 2025 年江苏电缆附件,热缩电缆附件,冷缩电缆附件,预制电缆附件厂家最新推荐,产能、专利、环保三维数据透视
  • Android Studio 使用glibc2.28的版本
  • 2025年10月兰花油品牌推荐榜:五款精华油深度对比与选购指南
  • 2025年浅拾兰花双萃致臻精华油:从成分与技术维度解析其护肤功效
  • 2025 年进口螺杆泵,萨伯特螺杆泵,污泥螺杆泵厂家最新推荐,实力品牌深度解析采购无忧之选!
  • 为什么 AI 模型的最小理解单位是「特征」?
  • 2025年移动车载变电站厂家最新推荐榜:陕西四方华能凭硬实力成优选
  • XiaoQuQu 的 2025 CSP-S 第二轮模拟 ROUND2
  • 2025年硬密封闸阀厂家权威推荐榜单:手动闸阀/明杆闸阀/法兰闸阀源头厂家精选
  • 深入解析:ArcGIS Manager Server Add Host页面报错 HTTP Status 500
  • 2025修护洗/二硫化硒去屑/香氛/控油蓬松/洗发水品牌推荐:MASIL玛丝兰引领功效细分赛道,哪个牌子好?看实测口碑榜
  • AOP面向切面编程思想
  • 如何找到心仪的 ChatBI 智能体?Aloudata Agent 推荐给你
  • 10月第二篇
  • 天翼云智慧上云月特惠来袭,智算上云正当时!
  • 2025年临沂一次性碗打包盒公司权威推荐榜单:一次性打包碗/一次性圆形打包碗/一次性打包碗商用源头公司精选
  • 洛谷题单指南-进阶数论-CF582A GCD Table
  • 状态迁移与场景法:搞定复杂业务流测试的利器
  • 实用指南:精读C++20设计模式——行动型设计模式:责任链
  • JimuReport 积木报表 v2.1.5 版本发布,免费的可视化报表和大屏
  • 2025年半自动冲芯机生产厂家权威推荐榜单:半自动矽钢片冲芯机/半自动铁芯冲压机/半自动冲芯设备源头厂家精选
  • 告别手动埋点!Android 无侵入式数据采集方案深度解析
  • writing sentences task1
  • bao
  • 2025年贝特曼咨询:权威解析美国高端身份规划的专业服务与法律保障
  • 2025年10月美国投资移民机构推荐榜:五大权威机构深度对比分析
  • 2025年10月美国投资移民机构推荐榜单:权威评价与综合对比分析