当前位置: 首页 > 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.hskmm.com/?act=detail&tid=40633

相关文章:

  • 2025 年氧化铝源头厂家最新推荐排行榜:聚焦高纯低放射性单晶等优质产品,优选具备国际化品质保障企业精密抛光/99.9%/正极包覆/高纯活性氧化铝公司推荐
  • 玩转LuatOS GNSS:定位初启、NMEA数据处理与实时上报秘籍
  • tensor RT 进行gpu推理加速/模型部署
  • MySQL 存储过程
  • Dynamics 365 online 按钮配置地址:/main.aspx?settingsonly=true
  • 替换法和sympy解方程5例
  • 2025 年浴室柜厂家最新推荐榜,技术实力与市场口碑深度解析
  • centos7编译安装openssl3.4
  • 2025 年 10 月石墨加工厂家推荐排行榜,高纯石墨加工,精密石墨加工,耐高温石墨加工,异形石墨加工公司推荐
  • 安装GMSSL时报错is not able to compie a sinple test program
  • 2025年口碑好的铜芯电缆品牌排行榜:鑫佰亿线缆引领行业品质革命
  • 2025年口碑好的铜芯电缆品牌推荐榜单
  • 2025:智能体元年|国内智能体培训机构优劣势对比
  • 2025年口碑好的化工设备品牌排名前十
  • 2025年反应釜品牌排行榜:江苏永润反应釜荣获第一
  • 2025年换热器品牌综合评测:江苏永润换热器领跑行业
  • 2025年口碑好的天然石材厂家排名前十推荐
  • 2025铝合金/装配式/工业/仓储/体育/场馆/机库/矿业/篷房厂家推荐榜:华烨海特斯以安全与定制引领行业变革
  • 2025 年注塑机定制厂家最新推荐榜,技术实力与市场口碑深度解析,甄选高精度节能优质品牌专用注塑机/瓶盖专用注塑机/电动工具专用注塑机公司推荐
  • 2025年小红书代运营/营销/推广/种草/探店推荐榜:广州布马五星领跑!全链路种草 + 数据转化,另2家公司凭垂类 / 联动 / 性价比显实力
  • 2025年市面上碳晶板品牌综合评测排行榜
  • 2025年口碑好的工装冰火板品牌综合评测与排名
  • 机器学习之支持向量机SVM
  • 2025年北京无需审核的小额贷款公司权威推荐榜单:小额贷款/企业小额贷款/申请小额贷款源头公司精选
  • 多功能视频处理工具:轻松搞定提音频、转 GIF、截图与合并
  • 2025年阻燃输送带生产厂家权威推荐榜单:尼龙输送带/三叶输送带/输送带源头厂家精选
  • 2025年水平桥架源头厂家排行榜前十强
  • 2025年水平桥架供应厂家推荐榜:顶级品牌盘点
  • 《导航切换》案例
  • 技术探究:Air8000工业引擎赋能的WiFi AP文件管理系统实现剖析!