先上原链接,一个很不错的wpf图标库 :
https://github.com/MahApps/MahApps.Metro.IconPacks
提供了大量的图标可以用,如下图:(部分截图)
简单分析了一下代码,
并模仿它写一个图标类和简单的使用示例:
App.xaml:
<Application x:Class="IconPackMini.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:IconPackMini"StartupUri="MainWindow.xaml"><Application.Resources><Style TargetType="local:IconButton"><Setter Property="FontFamily" Value="微软雅黑"/><Setter Property="FontSize" Value="14"/><Setter Property="Foreground" Value="Black"/><Setter Property="BorderThickness" Value="0"/><Setter Property="Margin" Value="0"/><Setter Property="Padding" Value="0"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/><Setter Property="SnapsToDevicePixels" Value="True"/><Setter Property="UseLayoutRounding" Value="True"/><Setter Property="Background" Value="Transparent"/><Setter Property="SnapsToDevicePixels" Value="True"/><Setter Property="Padding" Value="5"/><Setter Property="Cursor" Value="Hand"/><Setter Property="Width" Value="32"/><Setter Property="Height" Value="{Binding ActualWidth,RelativeSource={RelativeSource Self}}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="local:IconButton"><Border x:Name="_border" CornerRadius="{TemplateBinding CornerRadius}"ClipToBounds="True"BorderThickness="{TemplateBinding BorderThickness}"BorderBrush="{TemplateBinding BorderBrush}"Background="{TemplateBinding Background}"><Viewbox Margin="{TemplateBinding Padding}"Stretch="Uniform"><Path x:Name="_path"Width="{TemplateBinding PathWidth}"Height="{TemplateBinding PathHeight}"Fill="{TemplateBinding Foreground}" Data="{TemplateBinding Geometry}"/></Viewbox></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True" SourceName="_border"><Setter Property="Background" Value="{Binding HoverBackground,RelativeSource={RelativeSource AncestorType=local:IconButton}}" TargetName="_border"/><Setter Property="Fill" Value="{Binding HoverForeground,RelativeSource={RelativeSource AncestorType=local:IconButton}}" TargetName="_path"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style></Application.Resources>
</Application>
iconKey 枚举类:
namespace IconPackMini
{public enum IconKey{None,/// <summary>/// 关闭按钮/// </summary>Close,/// <summary>/// 最小化按钮/// </summary>MinSize,/// <summary>/// 普通最大化按钮/// </summary>MaxSize,/// <summary>/// 全屏后最大化按钮/// </summary>MaxToNormal,/// <summary>/// 设置按钮/// </summary>Setting,/// <summary>/// 五角星/// </summary>Star,}
}
还有大部分控件类代码,都放在仓库内,自取。
扩展也很简单,使用时,取得path后,添加新图标到IconKey 和IconKeyDic内就可以了,对于不想引用类库又想用的人,还行。
效果图:
【原创】转载请注明出处。
【加群】要加入 WPF UI 微信群的,可以添加我的微信。
【资源】代码仓库地址:https://gitee.com/gxygitee/pub.git