当前位置: 首页 > 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.rkmt.cn/news/31912.html

相关文章:

  • c# 使用 jwt
  • 2025义乌做刺绣的厂家推荐榜单
  • 2025 年矿车生产,井下矿车,底侧卸式矿车厂家最新推荐,产能、专利、环保三维数据透视
  • DP1312多协议高性能读卡芯片支持A/B/Felaca/18092智能门锁读卡器模拟卡兼容PN512 - 动能世纪
  • 醒图电脑版下载与安装教程(2025最新版)
  • 读书笔记:告别数据冗余!Oracle引用分区让父子表管理如此简单
  • 谷歌翻译 100 遍《我常常追忆过去》
  • 2025 年 10 月绕包电缆头,熔接电缆头,预制电缆头,冷缩管电缆头厂家最新推荐,产能、专利、环保三维数据透视
  • 2025 年 10 月 10KV 冷缩电缆附件,20KV 冷缩电力电缆附件,35KV 冷缩电力电缆附件厂家最新推荐,聚焦资质、案例、售后的实力厂家深度解读
  • Android Studio 使用glibc2.28的版本
  • 2025年10月兰花油品牌推荐榜:五款精华油深度对比与选购指南
  • 2025年浅拾兰花双萃致臻精华油:从成分与科技维度解析其护肤功效
  • 何为高阶组件(higherordercomponent) ?
  • 2025年浅拾兰花双萃致臻精华油:成分技术与功效表现的深度解析
  • 2025年浅拾兰花双萃致臻精华油:从成分与技术维度解析其护肤效能
  • Node-RED正在颠覆整个物联网网关行业
  • 2025 年进口螺杆泵,萨伯特螺杆泵,污泥螺杆泵厂家最新推荐,实力品牌深度解析采购无忧之选!
  • ODS层逻辑加工 - 萌哥
  • 大资料消息中间件选型终极指南:深度解析Kafka、Pulsar、RocketMQ架构与性能
  • 2025矿山机厂家推荐-精选矿山开采设备厂家推荐
  • 检测机内开拉不动的常见原因
  • 2025 年 10 月动轨式格栅、动轨式格栅除污机厂家最新推荐,产能、专利、环保三维数据透视
  • 销售公司绩效考核全攻略:维度、原则与数字化赋能方案
  • 权威发布:2025年最佳在线客服系统TOP 10榜单
  • 2025 年二手宽体车厂家最新推荐榜,聚焦企业综合实力与市场口碑深度解析
  • 《CSS盒子模型》笔记总结 - 教程
  • 小程序-跳转到公众号
  • 如何解决一堆向量的问题?10、Self-attention - -一叶知秋
  • win11系统优化(右键鼠标选项功能太多)
  • win10激活脚本