using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; namespace BPA.UIControl { /// /// 图标 /// public class Icon : Control { private static readonly Lazy> _codes = new Lazy>(IconDatas.GetAll); /// /// Initializes a new instance of the class. /// static Icon() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Icon), new FrameworkPropertyMetadata(typeof(Icon))); } /// public override void OnApplyTemplate() { base.OnApplyTemplate(); UpdateIcon(); } /// /// 图标类型 /// public static readonly DependencyProperty TypeProperty = DependencyProperty.Register( "Type", typeof(IconType), typeof(Icon), new PropertyMetadata(default(IconType), TypePropertyChangedCallBack)); private static void TypePropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((Icon)d).UpdateIcon(); /// /// 图标类型 /// public IconType Type { get { return (IconType)GetValue(TypeProperty); } set { SetValue(TypeProperty, value); } } /// /// 图标编码 /// public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code", typeof(string), typeof(Icon), new PropertyMetadata("")); /// /// 图标编码 /// public string Code { get { return (string)GetValue(CodeProperty); } set { SetValue(CodeProperty, value); } } /// /// 组 /// public static readonly DependencyProperty GroupProperty = DependencyProperty.Register("Group", typeof(string), typeof(Icon), new PropertyMetadata("")); /// /// 组 /// public string Group { get { return (string)GetValue(GroupProperty); } set { SetValue(GroupProperty, value); } } private void UpdateIcon() { var icon = new IconInfo(string.Empty, string.Empty); _codes.Value?.TryGetValue(Type, out icon); if (icon != null) { Group = icon.Group; Code = icon.Data; } else { } } /// /// 获取所有图标信息 /// /// 所有图标信息 public static IEnumerable GetAllIconInfo() => _codes.Value.Select(x => new IconInfo(x.Key, x.Value.Group, x.Value.Data)); } /// /// 图标信息 /// public class IconInfo { /// /// 图标组 /// public string Group { get; set; } /// /// 图标数据 /// public string Data { get; set; } /// /// 图标类型 /// public IconType Type { get; set; } /// /// 图标信息 /// /// 图标组 /// 图标数据 public IconInfo(string group, string data) { Group = group; Data = data; } /// /// 图标信息 /// /// 图标类型 /// 图标组 /// 图标数据 public IconInfo(IconType type, string group, string data) : this(group, data) { Type = type; } } }