You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace BPA.UIControl
  7. {
  8. /// <summary>
  9. /// 图标
  10. /// </summary>
  11. public class Icon : Control
  12. {
  13. private static readonly Lazy<Dictionary<IconType, IconInfo>> _codes = new Lazy<Dictionary<IconType, IconInfo>>(IconDatas.GetAll);
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="Icon"/> class.
  16. /// </summary>
  17. static Icon()
  18. {
  19. DefaultStyleKeyProperty.OverrideMetadata(typeof(Icon), new FrameworkPropertyMetadata(typeof(Icon)));
  20. }
  21. /// <inheritdoc/>
  22. public override void OnApplyTemplate()
  23. {
  24. base.OnApplyTemplate();
  25. UpdateIcon();
  26. }
  27. /// <summary>
  28. /// 图标类型
  29. /// </summary>
  30. public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
  31. "Type", typeof(IconType), typeof(Icon), new PropertyMetadata(default(IconType), TypePropertyChangedCallBack));
  32. private static void TypePropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((Icon)d).UpdateIcon();
  33. /// <summary>
  34. /// 图标类型
  35. /// </summary>
  36. public IconType Type
  37. {
  38. get { return (IconType)GetValue(TypeProperty); }
  39. set { SetValue(TypeProperty, value); }
  40. }
  41. /// <summary>
  42. /// 图标编码
  43. /// </summary>
  44. public static readonly DependencyProperty CodeProperty =
  45. DependencyProperty.Register("Code", typeof(string), typeof(Icon), new PropertyMetadata(""));
  46. /// <summary>
  47. /// 图标编码
  48. /// </summary>
  49. public string Code
  50. {
  51. get { return (string)GetValue(CodeProperty); }
  52. set { SetValue(CodeProperty, value); }
  53. }
  54. /// <summary>
  55. /// 组
  56. /// </summary>
  57. public static readonly DependencyProperty GroupProperty =
  58. DependencyProperty.Register("Group", typeof(string), typeof(Icon), new PropertyMetadata(""));
  59. /// <summary>
  60. /// 组
  61. /// </summary>
  62. public string Group
  63. {
  64. get { return (string)GetValue(GroupProperty); }
  65. set { SetValue(GroupProperty, value); }
  66. }
  67. private void UpdateIcon()
  68. {
  69. var icon = new IconInfo(string.Empty, string.Empty);
  70. _codes.Value?.TryGetValue(Type, out icon);
  71. if (icon != null)
  72. {
  73. Group = icon.Group;
  74. Code = icon.Data;
  75. }
  76. else
  77. {
  78. }
  79. }
  80. /// <summary>
  81. /// 获取所有图标信息
  82. /// </summary>
  83. /// <returns>所有图标信息</returns>
  84. public static IEnumerable<IconInfo> GetAllIconInfo() => _codes.Value.Select(x => new IconInfo(x.Key, x.Value.Group, x.Value.Data));
  85. }
  86. /// <summary>
  87. /// 图标信息
  88. /// </summary>
  89. public class IconInfo
  90. {
  91. /// <summary>
  92. /// 图标组
  93. /// </summary>
  94. public string Group { get; set; }
  95. /// <summary>
  96. /// 图标数据
  97. /// </summary>
  98. public string Data { get; set; }
  99. /// <summary>
  100. /// 图标类型
  101. /// </summary>
  102. public IconType Type { get; set; }
  103. /// <summary>
  104. /// 图标信息
  105. /// </summary>
  106. /// <param name="group">图标组</param>
  107. /// <param name="data">图标数据</param>
  108. public IconInfo(string group, string data)
  109. {
  110. Group = group;
  111. Data = data;
  112. }
  113. /// <summary>
  114. /// 图标信息
  115. /// </summary>
  116. /// <param name="type">图标类型</param>
  117. /// <param name="group">图标组</param>
  118. /// <param name="data">图标数据</param>
  119. public IconInfo(IconType type, string group, string data)
  120. : this(group, data)
  121. {
  122. Type = type;
  123. }
  124. }
  125. }