MetaPackIcon.xaml
<UserControl
x:Class="FirstSolver.MetaPackIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c2vcommon="clr-namespace:FirstSolver"
Name="UserControlPackIcon">
<UserControl.Resources>
<DataTemplate x:Key="keyPath">
<Path Stretch="Uniform" Stroke="{Binding Foreground,ElementName=UserControlPackIcon}" StrokeThickness="{Binding StrokeThickness,ElementName=UserControlPackIcon}" Data="{Binding Data,ElementName=UserControlPackIcon}"/>
</DataTemplate>
<DataTemplate x:Key="keyImage">
<Image Stretch="Uniform" Source="{Binding Image,ElementName=UserControlPackIcon}"/>
</DataTemplate>
</UserControl.Resources>
<UserControl.ContentTemplateSelector>
<c2vcommon:MetaPackIconDataTemplateSelector PathDataTemplate="{StaticResource keyPath}" ImageDataTemplate="{StaticResource keyImage}"/>
</UserControl.ContentTemplateSelector>
</UserControl>
MetaPackIcon.xaml.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace FirstSolver
{
/// <summary>
/// 多格式图标:
/// 1.支持Path、PNG三种格式
/// 2.配置文件:SVG\MetaPackIcon.json
/// </summary>
public partial class MetaPackIcon : UserControl
{
private static readonly Lazy<IDictionary<string, string>> s_holder = new Lazy<IDictionary<string, string>>(() => { return JsonConvert.DeserializeObject<Dictionary<string, string>>(System.IO.File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}SVG\\{nameof(MetaPackIcon)}.json")) ?? new Dictionary<string, string>(); });
/// <summary>
/// SVG图标库源
/// </summary>
public static IDictionary<string, string> ItemsSource => s_holder.Value;
/// <summary>
/// 构造函数
/// </summary>
public MetaPackIcon() => InitializeComponent();
/// <summary>
/// 是否是间接数据
/// </summary>
public bool IsInderect { get; private set; }
/// <summary>
/// 图像
/// </summary>
public BitmapImage Image { get; private set; }
#region Kind
/// <summary>
/// 自定义依赖属性:KindProperty
/// </summary>
public static readonly DependencyProperty KindProperty = DependencyProperty.Register(
nameof(Kind),
typeof(string),
typeof(MetaPackIcon),
new FrameworkPropertyMetadata(
default(string),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback((DependencyObject d, DependencyPropertyChangedEventArgs e) =>
{
if (d is MetaPackIcon icon)
{
if (ItemsSource.TryGetValue(icon.Kind, out string source))
{
if (source.StartsWith("PNG:"))
{ // 图像
icon.IsInderect = true;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = new System.IO.MemoryStream(System.IO.File.ReadAllBytes($"{AppDomain.CurrentDomain.BaseDirectory}SVG\\{source.Substring(4)}"));
bi.EndInit();
bi.Freeze();
icon.Image = bi;
}
else
{ // Path路径数据
icon.IsInderect = false;
icon.Data = source;
}
}
}
})));
/// <summary>
/// 自定义属性:标识 Kind
/// </summary>
public string Kind
{
get { return (string)GetValue(KindProperty); }
set { SetValue(KindProperty, value); }
}
#endregion
#region Data
private static readonly DependencyPropertyKey DataPropertyKey = DependencyProperty.RegisterReadOnly(nameof(Data), typeof(string), typeof(MetaPackIcon), new PropertyMetadata(string.Empty));
/// <summary>
/// 自定义依赖属性:DataProperty
/// </summary>
public static readonly DependencyProperty DataProperty = DataPropertyKey.DependencyProperty;
/// <summary>
/// 自定义属性:路径数据 Data
/// </summary>
[TypeConverter(typeof(GeometryConverter))]
public string Data
{
get => (string)GetValue(DataProperty);
private set => SetValue(DataPropertyKey, value);
}
#endregion
#region StrokeThickness
/// <summary>
/// 自定义依赖属性:StrokeThickness
/// </summary>
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
nameof(StrokeThickness),
typeof(double),
typeof(MetaPackIcon),
new PropertyMetadata(1.0));
/// <summary>
/// 自定义属性:笔画粗细 StrokeThickness
/// </summary>
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
#endregion
#region IsFill
/// <summary>
/// 自定义依赖属性:IsFillProperty
/// </summary>
public static readonly DependencyProperty IsFillProperty = DependencyProperty.Register(
nameof(IsFill),
typeof(bool),
typeof(MetaPackIcon),
new PropertyMetadata(false));
/// <summary>
/// 自定义属性:是否填充 IsFill
/// </summary>
public bool IsFill
{
get => (bool)GetValue(IsFillProperty);
set => SetValue(IsFillProperty, value);
}
#endregion
/// <summary>
/// 呈现
/// </summary>
/// <param name="drawingContext">绘图上下文</param>
protected override void OnRender(DrawingContext drawingContext)
{
var child = C2VUserControl.GetChild<System.Windows.Shapes.Path>(this, null);
if (child != null)
{
child.Fill = IsFill ? child.Stroke : Brushes.Transparent;
}
base.OnRender(drawingContext);
}
}
}
MetaPackIconDataTemplateSelector.cs
using System.Windows;
using System.Windows.Controls;
namespace FirstSolver
{
/// <summary>
/// MetaPackIcon的数据模板转换器
/// </summary>
internal class MetaPackIconDataTemplateSelector : DataTemplateSelector
{
/// <summary>
/// 整型值数据模板
/// </summary>
public DataTemplate PathDataTemplate { get; set; }
/// <summary>
/// 布尔值数据模板
/// </summary>
public DataTemplate ImageDataTemplate { get; set; }
/// <summary>
/// 选择模板
/// </summary>
/// <param name="item">输入项</param>
/// <param name="container">依赖对象</param>
/// <returns>对应输入项的数据模板</returns>
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var icon = C2VUserControl.GetParent<MetaPackIcon>(container, null);
return icon.IsInderect ? ImageDataTemplate : PathDataTemplate;
}
}
}
MetaPackIcon.json
{
"盾牌": "M12 2L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-3z",
"金毛": "PNG:金毛.png",
"箭头": "M514.829442 971.96488c-13.113669 0-25.545816-5.195326-35.031864-14.634302L372.123252 854.516958c-9.530051-9.501398-14.778588-21.99392-14.778588-35.175127 0-27.526937 22.395056-49.921993 49.921993-49.921993l44.10859 0L451.375248 570.301847 253.599835 570.301847l0 47.591925c0 27.526937-22.395056 49.921993-49.921993 49.921993-13.163811 0-25.640983-5.234211-35.139312-14.740726L57.574277 543.413454c-9.578146-9.550517-14.826684-22.042016-14.826684-35.223223 0-13.180184 5.247514-25.671683 14.777565-35.175127l111.032816-110.234637c9.448186-9.474792 21.940708-14.722306 35.120892-14.722306 27.526937 0 49.921993 22.395056 49.921993 49.921993l0 48.752354 196.614983 0L450.215842 246.961648l-42.948161 0c-27.526937 0-49.921993-22.395056-49.921993-49.921993 0-13.180184 5.247514-25.671683 14.777565-35.175127l0.672312-0.63138 107.232257-94.791923c9.455349-9.295714 21.793352-14.407128 34.802644-14.407128 12.966313 0 25.266453 5.078669 34.710546 14.3181l110.052488 94.298689 0.410346 0.409322c9.530051 9.502421 14.776542 21.994943 14.776542 35.175127 0 27.488051-22.333658 49.858548-49.808406 49.921993L578.427922 246.763127l0 198.808952 197.631126 0 0-45.270043c0-27.526937 22.395056-49.921993 49.921993-49.921993 13.180184 0 25.671683 5.247514 35.174104 14.777565l0.113587 0.11461 105.275695 107.813494c9.485025 9.494235 14.706957 21.957081 14.706957 35.105542 0 13.120832-5.200442 25.560142-14.648628 35.046191L861.155145 653.0382c-9.502421 9.530051-21.99392 14.776542-35.174104 14.776542-27.526937 0-49.921993-22.395056-49.921993-49.921993L776.059048 570.301847 578.427922 570.301847l0 199.11799 46.430472 0c27.526937 0 49.921993 22.395056 49.921993 49.921993 0 13.181207-5.248537 25.673729-14.777565 35.175127l-0.363274 0.352017L549.793768 957.397093C540.316929 966.794114 527.912411 971.96488 514.829442 971.96488zM393.69557 832.673463 501.392408 935.509596c3.701299 3.711532 8.473999 5.756097 13.438057 5.756097 4.963035 0 9.735735-2.044566 13.437034-5.756097l0.394996-0.382717 109.820198-102.505605c3.612271-3.677762 5.599532-8.384971 5.599532-13.280468 0-10.420327-8.803503-19.222807-19.222807-19.222807L547.728736 800.118 547.728736 539.602661l259.029498 0 0 78.291111c0 10.420327 8.803503 19.222807 19.222807 19.222807 4.924149 0 9.658987-2.01182 13.348006-5.666046l105.468077-109.823268c3.711532-3.700275 5.755074-8.471952 5.755074-13.43601s-2.043542-9.736758-5.755074-13.437034l-0.144286-0.145309L839.364863 386.782114c-3.693112-3.678786-8.444323-5.702885-13.383822-5.702885-10.420327 0-19.222807 8.803503-19.222807 19.222807l0 75.969229L547.728736 476.271264 547.728736 216.460983l77.129658-1.00284c10.420327 0 19.222807-8.803503 19.222807-19.222807 0-4.805446-1.915629-9.431813-5.405103-13.0799L528.691147 88.915076l-0.424672-0.426719c-3.701299-3.711532-8.472975-5.756097-13.437034-5.756097s-9.736758 2.044566-13.438057 5.756097l-0.703011 0.661056-107.169835 94.736665c-3.533476 3.659343-5.475711 8.314363-5.475711 13.152555 0 10.419304 8.803503 19.222807 19.222807 19.222807l73.647347 0 0 261.169231L222.900649 477.43067l0-79.45154c0-10.419304-8.80248-19.222807-19.222807-19.222807-4.964058 0-9.735735 2.044566-13.437034 5.755074L79.178317 494.777757c-3.687996 3.676739-5.731538 8.449439-5.731538 13.412474s2.044566 9.735735 5.755074 13.43601l111.038956 109.73424c3.701299 3.711532 8.472975 5.755074 13.437034 5.755074 10.420327 0 19.222807-8.803503 19.222807-19.222807L222.900649 539.602661l259.173784 0 0 260.516362-74.807776 0c-10.419304 0-19.222807 8.803503-19.222807 19.222807C388.04385 824.25984 390.04953 828.988538 393.69557 832.673463z"
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。