WPF 模仿IconPacks库写图标按钮

先上原链接,一个很不错的wpf图标库 : 

https://github.com/MahApps/MahApps.Metro.IconPacks

提供了大量的图标可以用,如下图:(部分截图)

5670b5faf33d96e36314ab5bc25c85a9.png

简单分析了一下代码,

并模仿它写一个图标类和简单的使用示例:

2430a2dc759024529dc5799447873c7e.png

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内就可以了,对于不想引用类库又想用的人,还行。

效果图:

c249fe649816b1412d8104a6d9aff038.png

【原创】转载请注明出处。

【加群】要加入 WPF UI 微信群的,可以添加我的微信。

【资源】代码仓库地址:https://gitee.com/gxygitee/pub.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值