尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

样式资源键-独立的控件库

样式资源键-独立的控件库
📅 发布时间:2026/6/20 23:13:20

自定义样式,供其他项目调用

第一步创建一个控件库项目

第二步创建一个资源键类

using System.Windows;
namespace SharedStyles;public static class ButtonKeys
{// 主按钮样式资源键(强类型定义)public static ComponentResourceKey PrimaryButtonKey =>new ComponentResourceKey(typeof(ButtonKeys), "PrimaryButton");// 次要按钮样式资源键(强类型定义)public static ComponentResourceKey SecondaryButtonKey =>new ComponentResourceKey(typeof(ButtonKeys), "SecondaryButton");
}

第三步创建前台代码,建立一个xaml文件。注意:以下代码引用键的方式有两种 一个是静态引用,一个是直接用,推荐静态引用(可以帮你检查代码有无错误).

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:SharedStyles"><!-- 主按钮样式(使用x:Static引用资源键) --><Style x:Key="{x:Static local:ButtonKeys.PrimaryButtonKey}" TargetType="Button"><Setter Property="Background" Value="Green" /><Setter Property="Foreground" Value="White" /><Setter Property="Padding" Value="12,6" /><Setter Property="FontWeight" Value="Bold" /><Setter Property="Margin" Value="5" /><!-- 重写模板,避免默认样式干扰 --><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><!-- 用Border作为按钮的视觉容器 --><Border x:Name="buttonBorder" Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Padding="{TemplateBinding Padding}"CornerRadius="4"><ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /></Border><!-- 模板内的触发器(优先级高于样式触发器) --><ControlTemplate.Triggers><!-- 鼠标悬停时改变背景 --><Trigger Property="IsMouseOver" Value="True"><Setter TargetName="buttonBorder" Property="Background" Value="Red" /></Trigger><!-- 可选:添加点击状态 --><Trigger Property="IsPressed" Value="True"><Setter TargetName="buttonBorder" Property="Background" Value="DarkRed" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><!-- 次要按钮样式(直接用ComponentResourceKey定义) --><Style x:Key="{ComponentResourceKey ResourceId=SecondaryButton, TypeInTargetAssembly={x:Type local:ButtonKeys}}" TargetType="Button"><Setter Property="Background" Value="#F5F5F5" /><Setter Property="Foreground" Value="#333" /><Setter Property="Padding" Value="12,6" /><Setter Property="Margin" Value="5" /><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="#E0E0E0" /></Trigger></Style.Triggers></Style></ResourceDictionary>

以上一个控件库 程序集就建立好了。

下面是引用

第四步 创建一个普通的WPF窗体项目

1.命名空间要引用xmlns:shared="clr-namespace:SharedStyles;assembly=SharedStyles"

2.合并资源文件

3.使用资源样式

<Window x:Class="MainApp.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:shared="clr-namespace:SharedStyles;assembly=SharedStyles"mc:Ignorable="d"Title="主应用程序" Height="300" Width="400">
<!--也可以在APP.XAML中合并--><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><!-- 引用类库中的资源字典 --><ResourceDictionary Source="pack://application:,,,/SharedStyles;component/ButtonStyles.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock Text="使用共享样式的按钮" FontSize="16" HorizontalAlignment="Center"/><!-- 使用主按钮样式(x:Static引用) --><Button Content="主按钮" Style="{StaticResource {x:Static shared:ButtonKeys.PrimaryButtonKey}}" /><!-- 使用次要按钮样式(ComponentResourceKey直接引用) --><Button Content="次要按钮" Style="{StaticResource {ComponentResourceKey ResourceId=SecondaryButton, TypeInTargetAssembly={x:Type shared:ButtonKeys}}}" /></StackPanel>
</Window>

 

相关新闻

  • 玩转LuatOS GNSS:定位初启、NMEA数据处理与实时上报秘籍
  • 实用指南:cmake命令行工具介绍
  • 基于MATLAB的PIV(粒子图像测速) 实现方案

最新新闻

  • 本地AI Agent选型指南:无GPU、断网、零运维场景下的四大框架实测
  • Legacy iOS Kit终极指南:免费解锁旧iPhone/iPad完整控制权
  • 五艘无人艇分布式围捕编队控制仿真研究(Matlab代码实现)
  • Windows苹果设备驱动安装终极指南:3步实现iPhone网络共享
  • 2026六盘水防水补漏避坑指南:卫生间/厨房/阳台/屋顶/地下室漏水检测维修全攻略,正规施工+透明报价+口碑榜靠谱服务商推荐 - 安佳防水
  • emWin控件API实战:BUTTON与CHECKBOX的设计哲学与高级应用

日新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号