当前位置: 首页 > news >正文

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

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

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

第二步创建一个资源键类

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>

 

http://www.jsqmd.com/news/25811/

相关文章:

  • 频谱分析仪的应用范围与技术解析
  • windows下安装Nginx,并配置成服务
  • 2025年TPU材料生产厂家推荐:5大高品质、高性能厂家全解析
  • 2025年国内化工设备厂家/换热器/反应釜综合实力排行榜
  • 玩转LuatOS GNSS:定位初启、NMEA数据处理与实时上报秘籍
  • 实用指南:cmake命令行工具介绍
  • 2025 年 10 月石墨加工厂家推荐排行榜,高纯石墨加工,精密石墨加工,耐高温石墨加工,异形石墨加工公司推荐
  • tensor RT 进行gpu推理加速/模型部署
  • 安装GMSSL时报错is not able to compie a sinple test program
  • 【比赛记录】2025CSP+NOIP 冲刺模拟赛合集Ⅲ
  • Dynamics 365 online 按钮配置地址:/main.aspx?settingsonly=true
  • 替换法解方程5例
  • 什么是MII
  • 基于MATLAB的PIV(粒子图像测速) 实现方案
  • 祛魅与回归:对人工智能研究中“概念通胀”与“体系沉迷”的批判
  • 2025 年浴室柜厂家最新推荐榜,技术实力与市场口碑深度解析
  • 二分查找法
  • 2025 年卫浴厂家最新推荐榜,技术实力与市场口碑深度解析
  • 从餐馆迎客看 accept4:更灵活的“接客“高手 - 详解
  • 重生之我用AI写代码:前传——VSCode集成开源模型搭建智能开发环境
  • 2025年广东会议室话筒设备服务商权威推荐榜单:红外线会议话筒/会议麦克风扬声器/会议麦克风音响源头公司精选
  • 2025年口碑好的铜芯电缆公司排行榜:鑫佰亿线缆领跑行业
  • 2025年口碑好的铜芯电缆产品:鑫佰亿线缆(四川)有限公司领军行业
  • 2025年口碑好的铜芯电缆产品十大品牌权威推荐
  • 2025年口碑好的铜芯电缆品牌排行榜:鑫佰亿线缆引领行业品质革命
  • 2025年口碑好的铜芯电缆品牌推荐榜单
  • 2025年市面上新加坡留学品牌综合实力排行榜
  • 2025/10/27
  • 2025年电加热杀菌锅生产商权威推荐榜单:水浴式杀菌锅/高温高压杀菌锅/八宝粥杀菌锅源头厂家精选
  • 2025/10/28