自定义样式,供其他项目调用
第一步创建一个控件库项目
第二步创建一个资源键类
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>