WPF 关于图标的使用

一、关于图标的来源

          个人用iconfont-阿里巴巴矢量图标库获取,获取需要使用的图标,可以分项目加入图标,个人还是觉得很方便的。可以选择到自己心仪的图标,可以下载字体、图片、svg等。

二、使用引入字体的形式使用

           首先挑选完自己需要的图标,然后下载代码,找到ttf文件。然后在WPF项目中引入资源,就可以使用Unicode编码,就能将图标显示出来了。

<Window x:Class="WpfApp4.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:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <FontFamily x:Key="IconFont">pack://application:,,,/WpfApp4;component/iconfont.ttf#iconfont </FontFamily>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"> 
            <TextBlock FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62e;" />
            <TextBlock Margin="5 0" Foreground="LightBlue" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62d;" />
            <TextBlock Margin="5 0" Foreground="Red" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe638;" />
            <TextBlock Margin="5 0" Foreground="Green" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe600;" />
        </StackPanel>
    </Grid>
</Window>

效果图:

三、通过WPF的Path绘制Geometry来显示图标

将代码中的数据填入Geometry中,可以得到图形。

<Window x:Class="WpfApp4.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:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <FontFamily x:Key="IconFont">pack://application:,,,/WpfApp4;component/iconfont.ttf#iconfont </FontFamily>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"> 
            <TextBlock FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62e;" />
            <TextBlock Margin="5 0" Foreground="LightBlue" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62d;" />
            <TextBlock Margin="5 0" Foreground="Red" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe638;" />
            <TextBlock Margin="5 0" Foreground="Green" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe600;" />
        </StackPanel> 
        <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Viewbox  Width="32" Height="32">
                <Path Fill="Red">
                    <Path.Data>
                        <Geometry>
                            M839.168 580.096c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v238.592c0 2.048 0 3.584 0.512 5.632 0.512 2.048 0.512 9.216-2.048 12.288-1.536 1.536-5.632 3.584-14.336 3.584h-160.256l-1.024-169.472c1.024-8.704 2.56-37.376-14.848-58.368-7.68-9.216-22.016-19.968-46.08-19.968H481.28c-24.064 0-38.4 10.752-46.08 19.968-17.408 20.48-15.872 49.152-14.848 58.368l-1.024 168.96H258.56c-8.192 0-12.8-1.536-14.336-3.072-2.048-2.56-2.56-8.704-2.048-12.288 0.512-2.048 0.512-3.584 0.512-5.12v-238.592c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v230.912c-1.536 10.24-2.56 35.84 14.336 56.32 9.216 11.264 26.624 24.064 58.368 24.064h188.928c15.872 0 28.672-12.8 28.672-18.432l1.024-194.56c0-11.776 0-13.312-0.512-14.848-0.512-5.12 0-11.776 1.536-14.336h67.072c1.024 2.048 2.048 9.216 1.536 14.336-0.512 1.536-0.512 3.072-0.512 14.848l1.024 194.56c0 5.632 12.8 18.432 28.672 18.432h188.928c31.744 0 49.152-13.312 58.368-24.064 16.896-20.48 15.872-46.08 14.336-56.32l2.048-231.424z
                            M934.4 526.848L609.28 177.152c-6.656-7.168-39.936-43.008-90.112-45.056-36.352-1.024-71.168 15.36-102.912 49.152-47.104 49.152-151.552 160.256-311.808 329.728-9.216 9.728-14.336 15.36-14.848 15.872-10.752 11.264-10.24 29.696 1.024 40.96 5.12 5.12 12.288 7.68 19.968 7.68h1.024c7.68 0 14.848-3.584 19.968-9.216 0.512-0.512 6.144-6.656 15.872-16.896 159.744-168.96 263.68-279.552 309.76-328.192 19.968-20.992 39.936-31.744 58.88-31.232 27.136 0.512 47.104 23.552 48.64 25.088l1.024 1.024 326.144 350.208c5.632 5.632 13.312 9.216 20.992 9.216 7.168 0 14.336-2.56 19.456-7.68 11.776-11.264 12.8-29.184 2.048-40.96z 
                        </Geometry>
                    </Path.Data>
                </Path>
            </Viewbox>
            <Viewbox  Width="32" Height="32" Margin="5 0">
                <Path Fill="LightBlue">
                    <Path.Data>
                        <Geometry>
                            M819.182933 955.716267a34.1504 34.1504 0 0 1 0 68.283733H204.8a34.1504 34.1504 0 0 1 0-68.283733H819.2z m34.133334-955.716267C947.5072 0 1024 71.406933 1024 159.232v568.951467c-0.136533 87.842133-76.629333 159.232-170.683733 159.232H170.666667C76.6464 887.432533 0 816.042667 0 728.200533V159.249067C0 71.406933 76.629333 0 170.683733 0H853.333333z m0 68.283733H170.666667c-56.507733 0-102.4 40.789333-102.4 90.965334v568.9344c0 50.158933 45.892267 90.965333 102.4 90.965333h682.615466c56.5248 0 102.417067-40.8064 102.417067-90.965333V159.249067C955.733333 109.090133 909.824 68.266667 853.333333 68.266667zM578.816 310.442667l6.2464 3.584c11.52 7.0144 15.633067 22.528 9.233067 34.8672l-98.986667 184.098133c-5.9904 11.52-19.182933 16.1792-30.378667 11.144533l-2.7136-1.4336h0.068267l-6.314667-3.584c-11.52-7.031467-15.633067-22.528-9.233066-34.884266l98.986666-184.081067c6.724267-12.202667 21.4016-16.520533 33.092267-9.728z m-298.103467 4.676266a23.944533 23.944533 0 0 1 33.6384-1.877333l2.286934 2.218667c9.079467 9.898667 9.591467 25.463467 1.604266 36.010666l-1.8432 2.184534L248.32 426.666667l68.5568 73.045333a28.672 28.672 0 0 1-0.034133 38.4512c-9.216 9.898667-23.773867 10.5472-33.6896 1.9456l-2.048-1.979733-85.896534-91.767467a29.781333 29.781333 0 0 1-1.860266-37.137067l1.877333-2.269866 85.504-91.869867z m426.461867 0.034134a23.944533 23.944533 0 0 1 33.672533-1.928534l2.048 1.979734 85.879467 92.3136c4.949333 4.898133 7.68 11.8784 7.458133 19.114666 0.1024 6.365867-1.877333 12.509867-5.495466 17.442134l-1.962667 2.3552-85.879467 91.648c-9.2672 10.052267-23.671467 10.717867-33.6896 2.013866l-2.218666-2.184533a28.535467 28.535467 0 0 1-1.6896-35.976533l1.8432-2.184534 68.0448-73.1136-68.0448-73.0112a28.689067 28.689067 0 0 1 0.034133-38.468266z
                        </Geometry>
                    </Path.Data>
                </Path>
            </Viewbox>
        </StackPanel>
        

    </Grid>
</Window>

效果图:

四、基于上述我们可以自定义一个图标控件来显示和管理图标。

1、新建一个资源词典用于管理图标

这里举例选取了三个图标,代码如下

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Geometry x:Key="HomeGeometry" options:Freeze="True">
        M839.168 580.096c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v238.592c0 2.048 0 3.584 0.512 5.632 0.512 2.048 0.512 9.216-2.048 12.288-1.536 1.536-5.632 3.584-14.336 3.584h-160.256l-1.024-169.472c1.024-8.704 2.56-37.376-14.848-58.368-7.68-9.216-22.016-19.968-46.08-19.968H481.28c-24.064 0-38.4 10.752-46.08 19.968-17.408 20.48-15.872 49.152-14.848 58.368l-1.024 168.96H258.56c-8.192 0-12.8-1.536-14.336-3.072-2.048-2.56-2.56-8.704-2.048-12.288 0.512-2.048 0.512-3.584 0.512-5.12v-238.592c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v230.912c-1.536 10.24-2.56 35.84 14.336 56.32 9.216 11.264 26.624 24.064 58.368 24.064h188.928c15.872 0 28.672-12.8 28.672-18.432l1.024-194.56c0-11.776 0-13.312-0.512-14.848-0.512-5.12 0-11.776 1.536-14.336h67.072c1.024 2.048 2.048 9.216 1.536 14.336-0.512 1.536-0.512 3.072-0.512 14.848l1.024 194.56c0 5.632 12.8 18.432 28.672 18.432h188.928c31.744 0 49.152-13.312 58.368-24.064 16.896-20.48 15.872-46.08 14.336-56.32l2.048-231.424z
        M934.4 526.848L609.28 177.152c-6.656-7.168-39.936-43.008-90.112-45.056-36.352-1.024-71.168 15.36-102.912 49.152-47.104 49.152-151.552 160.256-311.808 329.728-9.216 9.728-14.336 15.36-14.848 15.872-10.752 11.264-10.24 29.696 1.024 40.96 5.12 5.12 12.288 7.68 19.968 7.68h1.024c7.68 0 14.848-3.584 19.968-9.216 0.512-0.512 6.144-6.656 15.872-16.896 159.744-168.96 263.68-279.552 309.76-328.192 19.968-20.992 39.936-31.744 58.88-31.232 27.136 0.512 47.104 23.552 48.64 25.088l1.024 1.024 326.144 350.208c5.632 5.632 13.312 9.216 20.992 9.216 7.168 0 14.336-2.56 19.456-7.68 11.776-11.264 12.8-29.184 2.048-40.96z
    </Geometry>

    <Geometry x:Key="ManagerGeometry" options:Freeze="True"> 
          M819.182933 955.716267a34.1504 34.1504 0 0 1 0 68.283733H204.8a34.1504 34.1504 0 0 1 0-68.283733H819.2z m34.133334-955.716267C947.5072 0 1024 71.406933 1024 159.232v568.951467c-0.136533 87.842133-76.629333 159.232-170.683733 159.232H170.666667C76.6464 887.432533 0 816.042667 0 728.200533V159.249067C0 71.406933 76.629333 0 170.683733 0H853.333333z m0 68.283733H170.666667c-56.507733 0-102.4 40.789333-102.4 90.965334v568.9344c0 50.158933 45.892267 90.965333 102.4 90.965333h682.615466c56.5248 0 102.417067-40.8064 102.417067-90.965333V159.249067C955.733333 109.090133 909.824 68.266667 853.333333 68.266667zM578.816 310.442667l6.2464 3.584c11.52 7.0144 15.633067 22.528 9.233067 34.8672l-98.986667 184.098133c-5.9904 11.52-19.182933 16.1792-30.378667 11.144533l-2.7136-1.4336h0.068267l-6.314667-3.584c-11.52-7.031467-15.633067-22.528-9.233066-34.884266l98.986666-184.081067c6.724267-12.202667 21.4016-16.520533 33.092267-9.728z m-298.103467 4.676266a23.944533 23.944533 0 0 1 33.6384-1.877333l2.286934 2.218667c9.079467 9.898667 9.591467 25.463467 1.604266 36.010666l-1.8432 2.184534L248.32 426.666667l68.5568 73.045333a28.672 28.672 0 0 1-0.034133 38.4512c-9.216 9.898667-23.773867 10.5472-33.6896 1.9456l-2.048-1.979733-85.896534-91.767467a29.781333 29.781333 0 0 1-1.860266-37.137067l1.877333-2.269866 85.504-91.869867z m426.461867 0.034134a23.944533 23.944533 0 0 1 33.672533-1.928534l2.048 1.979734 85.879467 92.3136c4.949333 4.898133 7.68 11.8784 7.458133 19.114666 0.1024 6.365867-1.877333 12.509867-5.495466 17.442134l-1.962667 2.3552-85.879467 91.648c-9.2672 10.052267-23.671467 10.717867-33.6896 2.013866l-2.218666-2.184533a28.535467 28.535467 0 0 1-1.6896-35.976533l1.8432-2.184534 68.0448-73.1136-68.0448-73.0112a28.689067 28.689067 0 0 1 0.034133-38.468266z
    </Geometry>

    <Geometry x:Key="SettingGeometry" options:Freeze="True">
            M602.94 928c-6.17 0-12.26-1.78-17.52-5.22a31.965 31.965 0 0 1-13.96-21.03c-5.24-28.69-30.25-49.51-59.46-49.51s-54.22 20.82-59.46 49.51a32 32 0 0 1-13.96 21.03 31.97 31.97 0 0 1-24.86 4.36c-44.47-10.49-86.77-28.03-125.75-52.13a32 32 0 0 1-9.5-45.4c16.56-23.99 13.59-56.38-7.05-77.03-20.65-20.64-53.04-23.61-77.03-7.05a32.034 32.034 0 0 1-24.74 4.99 32 32 0 0 1-20.66-14.49c-24.1-38.97-41.64-81.28-52.13-125.75a32.005 32.005 0 0 1 4.36-24.86c4.8-7.34 12.4-12.39 21.03-13.96 28.69-5.24 49.51-30.25 49.51-59.46s-20.82-54.22-49.51-59.46a32 32 0 0 1-21.03-13.96 31.97 31.97 0 0 1-4.36-24.86c10.49-44.47 28.03-86.78 52.13-125.75a32 32 0 0 1 45.4-9.5c23.99 16.56 56.38 13.59 77.03-7.05 20.65-20.65 23.61-53.04 7.05-77.03a32.034 32.034 0 0 1-4.99-24.74c1.8-8.58 7.04-16.05 14.49-20.66 38.97-24.1 81.28-41.64 125.75-52.13a31.97 31.97 0 0 1 24.86 4.36c7.34 4.8 12.39 12.4 13.96 21.03 5.24 28.69 30.25 49.51 59.46 49.51s54.22-20.82 59.46-49.51a32 32 0 0 1 13.96-21.03 31.97 31.97 0 0 1 24.86-4.36c44.47 10.49 86.77 28.03 125.75 52.13a32 32 0 0 1 9.5 45.4c-16.56 23.99-13.59 56.38 7.05 77.03 20.65 20.65 53.04 23.61 77.03 7.05a32.034 32.034 0 0 1 24.74-4.99 32 32 0 0 1 20.66 14.49c24.1 38.97 41.64 81.28 52.13 125.75 2.01 8.54 0.44 17.52-4.36 24.86a31.965 31.965 0 0 1-21.03 13.96c-28.69 5.24-49.51 30.25-49.51 59.46s20.82 54.22 49.51 59.46a32 32 0 0 1 21.03 13.96 31.97 31.97 0 0 1 4.36 24.86c-10.49 44.47-28.03 86.78-52.13 125.75a32 32 0 0 1-45.4 9.5c-23.99-16.56-56.38-13.6-77.03 7.05s-23.61 53.04-7.05 77.03a32.034 32.034 0 0 1 4.99 24.74 32.034 32.034 0 0 1-14.49 20.66c-38.97 24.1-81.28 41.64-125.75 52.13-2.42 0.58-4.89 0.86-7.34 0.86zM512 788.24c48.17 0 90.79 27.53 111.39 68.87a359.408 359.408 0 0 0 53.86-22.34c-14.65-43.79-3.97-93.38 30.08-127.44 34.05-34.05 83.65-44.73 127.44-30.08 8.9-17.34 16.37-35.34 22.34-53.86-41.34-20.6-68.87-63.22-68.87-111.39s27.53-90.79 68.87-111.39a358.363 358.363 0 0 0-22.34-53.86c-43.79 14.64-93.39 3.97-127.44-30.08s-44.73-83.64-30.08-127.43a359.408 359.408 0 0 0-53.86-22.34c-20.6 41.34-63.22 68.87-111.39 68.87s-90.79-27.53-111.39-68.87a359.408 359.408 0 0 0-53.86 22.34c14.65 43.79 3.97 93.38-30.08 127.44-24.09 24.09-55.97 36.48-88.06 36.48-13.27 0-26.57-2.12-39.38-6.4a359.408 359.408 0 0 0-22.34 53.86c41.34 20.6 68.87 63.22 68.87 111.39s-27.53 90.79-68.87 111.39a358.363 358.363 0 0 0 22.34 53.86c43.79-14.65 93.38-3.97 127.44 30.08 34.05 34.05 44.73 83.65 30.08 127.44 17.34 8.9 35.34 16.37 53.86 22.34 20.6-41.35 63.22-68.88 111.39-68.88z
            M512 751.19c-63.89 0-123.96-24.88-169.13-70.06-45.18-45.18-70.06-105.24-70.06-169.13s24.88-123.96 70.06-169.13c45.18-45.18 105.24-70.06 169.13-70.06s123.96 24.88 169.13 70.06c45.18 45.18 70.06 105.24 70.06 169.13s-24.88 123.96-70.06 169.13c-45.17 45.18-105.24 70.06-169.13 70.06z m0-414.38c-96.6 0-175.19 78.59-175.19 175.19S415.4 687.19 512 687.19 687.19 608.6 687.19 512 608.6 336.81 512 336.81z
            M512 607.13c-52.45 0-95.13-42.67-95.13-95.13s42.67-95.13 95.13-95.13 95.13 42.67 95.13 95.13-42.68 95.13-95.13 95.13z m0-126.26c-17.16 0-31.13 13.96-31.13 31.13s13.96 31.13 31.13 31.13 31.13-13.96 31.13-31.13-13.97-31.13-31.13-31.13z
            M480 568.65h64v150.5h-64zM545.048 455.95l130.324-75.245 32 55.424-130.324 75.245zM316.593 436.148l32-55.424 130.324 75.245-32 55.424z
    </Geometry>
</ResourceDictionary>
2、定义一个枚举方便对图标进行选择

      这里需要主要Description中的值与1中的Geometry中的Key的值对应

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp4
{
    public enum IconEnum
    {
        None,
        [Description("HomeGeometry")]
        Home,
        [Description("ManagerGeometry")]
        Manager,
        [Description("SettingGeometry")]
        Setting
    }
}
3、定义一个扩展方法,通过反射获取枚举的Description中的值
using System;
using System.ComponentModel;
using System.Reflection;

namespace WpfApp4
{
    public static class EnumExtension
    {
        public static string GetDescription(this Enum _enum)
        {
            Type type = _enum.GetType();
            FieldInfo field = type.GetField(_enum.ToString());
            object[] attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                DescriptionAttribute attribute = (DescriptionAttribute)attributes[0];
                return attribute != null ? attribute.Description : _enum.ToString();
            }
            return _enum.ToString();
        }
    }
}
4、新建一个自定义控件PathIcon

Xaml 代码,引入IconGeometry图形资源

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp4">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="./IconGeometry.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style  TargetType="{x:Type local:PathIcon}">
        <Setter Property="Padding" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Height" Value="16" /> 
        <Setter Property="Width" Value="16" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="Foreground">
            <Setter.Value>
                <Binding Path="Foreground" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Control}}" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:PathIcon}">
                    <Viewbox Margin="{TemplateBinding Padding}" 
                             HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                             VerticalAlignment="{TemplateBinding VerticalAlignment}"
                             UseLayoutRounding="True">
                        <Path x:Name="PART_Path" 
                              Fill="{TemplateBinding Foreground}"
                              Data="{TemplateBinding Data}"
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              Stretch="Uniform" UseLayoutRounding="False" />
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style> 
</ResourceDictionary>

cs部分代码

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApp4
{
    [TemplatePart(Name = "PART_Path", Type = typeof(Path))]
    public class PathIcon : Control
    {
        static PathIcon()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PathIcon), new FrameworkPropertyMetadata(typeof(PathIcon)));
        }

        //依赖属性 显示的图形
        public Geometry Data
        {
            get { return (Geometry)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(Geometry), typeof(PathIcon)); 

        /// <summary>
        /// 依赖属性用于绑定枚举 获取图形
        /// </summary>
        public IconEnum IconEnum
        {
            get { return (IconEnum)GetValue(IconEnumProperty); }
            set { SetValue(IconEnumProperty, value); }
        }
         
        public static readonly DependencyProperty IconEnumProperty =
            DependencyProperty.Register("IconEnum", typeof(IconEnum), typeof(PathIcon), new PropertyMetadata(IconEnum.None, OnIconEnumChanged));
         
        /// <summary>
        /// 枚举值发生变化时 获取图形、改变Data的值
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnIconEnumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var pathIcon = d as PathIcon;
            var iconEnum = (IconEnum)e.NewValue;
            if (iconEnum != IconEnum.None)
            {
                var enumDescription = iconEnum.GetDescription();
                pathIcon.Data = pathIcon.FindResource(enumDescription) as Geometry;
            }
        } 
    }
}

5、使用代码

<Window x:Class="WpfApp4.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:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources> 
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries> 
                <ResourceDictionary Source="pack://application:,,,/WpfApp4;component/PathIcon.xaml" />
            </ResourceDictionary.MergedDictionaries> 
            <FontFamily x:Key="IconFont">pack://application:,,,/WpfApp4;component/iconfont.ttf#iconfont</FontFamily>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition /> 
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"> 
            <TextBlock FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62e;" />
            <TextBlock Margin="5 0" Foreground="LightBlue" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe62d;" />
            <TextBlock Margin="5 0" Foreground="Red" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe638;" />
            <TextBlock Margin="5 0" Foreground="Green" FontSize="32" FontFamily="{DynamicResource IconFont}" Text="&#xe600;" />
        </StackPanel> 
        <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Viewbox  Width="32" Height="32">
                <Path Fill="Red">
                    <Path.Data>
                        <Geometry>
                            M839.168 580.096c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v238.592c0 2.048 0 3.584 0.512 5.632 0.512 2.048 0.512 9.216-2.048 12.288-1.536 1.536-5.632 3.584-14.336 3.584h-160.256l-1.024-169.472c1.024-8.704 2.56-37.376-14.848-58.368-7.68-9.216-22.016-19.968-46.08-19.968H481.28c-24.064 0-38.4 10.752-46.08 19.968-17.408 20.48-15.872 49.152-14.848 58.368l-1.024 168.96H258.56c-8.192 0-12.8-1.536-14.336-3.072-2.048-2.56-2.56-8.704-2.048-12.288 0.512-2.048 0.512-3.584 0.512-5.12v-238.592c0-15.872-12.8-28.672-28.672-28.672-15.872 0-28.672 12.8-28.672 28.672v230.912c-1.536 10.24-2.56 35.84 14.336 56.32 9.216 11.264 26.624 24.064 58.368 24.064h188.928c15.872 0 28.672-12.8 28.672-18.432l1.024-194.56c0-11.776 0-13.312-0.512-14.848-0.512-5.12 0-11.776 1.536-14.336h67.072c1.024 2.048 2.048 9.216 1.536 14.336-0.512 1.536-0.512 3.072-0.512 14.848l1.024 194.56c0 5.632 12.8 18.432 28.672 18.432h188.928c31.744 0 49.152-13.312 58.368-24.064 16.896-20.48 15.872-46.08 14.336-56.32l2.048-231.424z
                            M934.4 526.848L609.28 177.152c-6.656-7.168-39.936-43.008-90.112-45.056-36.352-1.024-71.168 15.36-102.912 49.152-47.104 49.152-151.552 160.256-311.808 329.728-9.216 9.728-14.336 15.36-14.848 15.872-10.752 11.264-10.24 29.696 1.024 40.96 5.12 5.12 12.288 7.68 19.968 7.68h1.024c7.68 0 14.848-3.584 19.968-9.216 0.512-0.512 6.144-6.656 15.872-16.896 159.744-168.96 263.68-279.552 309.76-328.192 19.968-20.992 39.936-31.744 58.88-31.232 27.136 0.512 47.104 23.552 48.64 25.088l1.024 1.024 326.144 350.208c5.632 5.632 13.312 9.216 20.992 9.216 7.168 0 14.336-2.56 19.456-7.68 11.776-11.264 12.8-29.184 2.048-40.96z 
                        </Geometry>
                    </Path.Data>
                </Path>
            </Viewbox>
            <Viewbox  Width="32" Height="32" Margin="5 0">
                <Path Fill="LightBlue">
                    <Path.Data>
                        <Geometry>
                            M819.182933 955.716267a34.1504 34.1504 0 0 1 0 68.283733H204.8a34.1504 34.1504 0 0 1 0-68.283733H819.2z m34.133334-955.716267C947.5072 0 1024 71.406933 1024 159.232v568.951467c-0.136533 87.842133-76.629333 159.232-170.683733 159.232H170.666667C76.6464 887.432533 0 816.042667 0 728.200533V159.249067C0 71.406933 76.629333 0 170.683733 0H853.333333z m0 68.283733H170.666667c-56.507733 0-102.4 40.789333-102.4 90.965334v568.9344c0 50.158933 45.892267 90.965333 102.4 90.965333h682.615466c56.5248 0 102.417067-40.8064 102.417067-90.965333V159.249067C955.733333 109.090133 909.824 68.266667 853.333333 68.266667zM578.816 310.442667l6.2464 3.584c11.52 7.0144 15.633067 22.528 9.233067 34.8672l-98.986667 184.098133c-5.9904 11.52-19.182933 16.1792-30.378667 11.144533l-2.7136-1.4336h0.068267l-6.314667-3.584c-11.52-7.031467-15.633067-22.528-9.233066-34.884266l98.986666-184.081067c6.724267-12.202667 21.4016-16.520533 33.092267-9.728z m-298.103467 4.676266a23.944533 23.944533 0 0 1 33.6384-1.877333l2.286934 2.218667c9.079467 9.898667 9.591467 25.463467 1.604266 36.010666l-1.8432 2.184534L248.32 426.666667l68.5568 73.045333a28.672 28.672 0 0 1-0.034133 38.4512c-9.216 9.898667-23.773867 10.5472-33.6896 1.9456l-2.048-1.979733-85.896534-91.767467a29.781333 29.781333 0 0 1-1.860266-37.137067l1.877333-2.269866 85.504-91.869867z m426.461867 0.034134a23.944533 23.944533 0 0 1 33.672533-1.928534l2.048 1.979734 85.879467 92.3136c4.949333 4.898133 7.68 11.8784 7.458133 19.114666 0.1024 6.365867-1.877333 12.509867-5.495466 17.442134l-1.962667 2.3552-85.879467 91.648c-9.2672 10.052267-23.671467 10.717867-33.6896 2.013866l-2.218666-2.184533a28.535467 28.535467 0 0 1-1.6896-35.976533l1.8432-2.184534 68.0448-73.1136-68.0448-73.0112a28.689067 28.689067 0 0 1 0.034133-38.468266z
                        </Geometry>
                    </Path.Data>
                </Path>
            </Viewbox>
        </StackPanel>
        <StackPanel Grid.Row="2" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
            <local:PathIcon IconEnum="Home" Foreground="LightBlue" />
            <local:PathIcon IconEnum="Manager" Foreground="Orange" Margin="5 0" />
            <local:PathIcon IconEnum="Setting" Foreground="Red" />

            <local:PathIcon Data="{StaticResource HomeGeometry}"  Margin="5 0"  />

            <local:PathIcon>
                <local:PathIcon.Data>
                    <Geometry>
                        M819.182933 955.716267a34.1504 34.1504 0 0 1 0 68.283733H204.8a34.1504 34.1504 0 0 1 0-68.283733H819.2z m34.133334-955.716267C947.5072 0 1024 71.406933 1024 159.232v568.951467c-0.136533 87.842133-76.629333 159.232-170.683733 159.232H170.666667C76.6464 887.432533 0 816.042667 0 728.200533V159.249067C0 71.406933 76.629333 0 170.683733 0H853.333333z m0 68.283733H170.666667c-56.507733 0-102.4 40.789333-102.4 90.965334v568.9344c0 50.158933 45.892267 90.965333 102.4 90.965333h682.615466c56.5248 0 102.417067-40.8064 102.417067-90.965333V159.249067C955.733333 109.090133 909.824 68.266667 853.333333 68.266667zM578.816 310.442667l6.2464 3.584c11.52 7.0144 15.633067 22.528 9.233067 34.8672l-98.986667 184.098133c-5.9904 11.52-19.182933 16.1792-30.378667 11.144533l-2.7136-1.4336h0.068267l-6.314667-3.584c-11.52-7.031467-15.633067-22.528-9.233066-34.884266l98.986666-184.081067c6.724267-12.202667 21.4016-16.520533 33.092267-9.728z m-298.103467 4.676266a23.944533 23.944533 0 0 1 33.6384-1.877333l2.286934 2.218667c9.079467 9.898667 9.591467 25.463467 1.604266 36.010666l-1.8432 2.184534L248.32 426.666667l68.5568 73.045333a28.672 28.672 0 0 1-0.034133 38.4512c-9.216 9.898667-23.773867 10.5472-33.6896 1.9456l-2.048-1.979733-85.896534-91.767467a29.781333 29.781333 0 0 1-1.860266-37.137067l1.877333-2.269866 85.504-91.869867z m426.461867 0.034134a23.944533 23.944533 0 0 1 33.672533-1.928534l2.048 1.979734 85.879467 92.3136c4.949333 4.898133 7.68 11.8784 7.458133 19.114666 0.1024 6.365867-1.877333 12.509867-5.495466 17.442134l-1.962667 2.3552-85.879467 91.648c-9.2672 10.052267-23.671467 10.717867-33.6896 2.013866l-2.218666-2.184533a28.535467 28.535467 0 0 1-1.6896-35.976533l1.8432-2.184534 68.0448-73.1136-68.0448-73.0112a28.689067 28.689067 0 0 1 0.034133-38.468266z
                    </Geometry>
                </local:PathIcon.Data>
            </local:PathIcon>
        </StackPanel> 
    </Grid>
</Window>

6、效果图

源码地址:https://download.csdn.net/download/qq_38060581/88800216

  • 22
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值