| 1 | using Core.Librarys.Image; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics; |
| 5 | using System.IO; |
| 6 | using System.Linq; |
| 7 | using System.Text; |
| 8 | using System.Threading.Tasks; |
| 9 | using System.Windows; |
| 10 | using System.Windows.Controls; |
| 11 | using System.Windows.Data; |
| 12 | using System.Windows.Documents; |
| 13 | using System.Windows.Input; |
| 14 | using System.Windows.Media; |
| 15 | using System.Windows.Media.Imaging; |
| 16 | using System.Windows.Navigation; |
| 17 | using System.Windows.Shapes; |
| 18 | using UI.Controls.Charts.Model; |
| 19 | |
| 20 | namespace UI.Controls.Charts |
| 21 | { |
| 22 | public class ChartsItemTypeList : Control |
| 23 | { |
| 24 | #region ChartsType 样式类型 |
| 25 | ///<summary> |
| 26 | /// |
| 27 | ///</summary> |
| 28 | public ChartsDataModel Data |
| 29 | { |
| 30 | get { return (ChartsDataModel)GetValue(DataProperty); } |
| 31 | set { SetValue(DataProperty, value); } |
| 32 | } |
| 33 | public static readonly DependencyProperty DataProperty = |
| 34 | DependencyProperty.Register("Data", |
| 35 | typeof(ChartsDataModel), |
| 36 | typeof(ChartsItemTypeList)); |
| 37 | |
| 38 | |
| 39 | #endregion |
| 40 | |
| 41 | #region MaxValue 最大值 |
| 42 | ///<summary> |
| 43 | /// |
| 44 | ///</summary> |
| 45 | public double MaxValue |
| 46 | { |
| 47 | get { return (double)GetValue(MaxValueProperty); } |
| 48 | set { SetValue(MaxValueProperty, value); } |
| 49 | } |
| 50 | public static readonly DependencyProperty MaxValueProperty = |
| 51 | DependencyProperty.Register("MaxValue", |
| 52 | typeof(double), |
| 53 | typeof(ChartsItemTypeList)); |
| 54 | |
| 55 | |
| 56 | #endregion |
| 57 | |
| 58 | #region IsLoading 是否正在加载中 |
| 59 | ///<summary> |
| 60 | /// |
| 61 | ///</summary> |
| 62 | public bool IsLoading |
| 63 | { |
| 64 | get { return (bool)GetValue(IsLoadingProperty); } |
| 65 | set { SetValue(IsLoadingProperty, value); } |
| 66 | } |
| 67 | public static readonly DependencyProperty IsLoadingProperty = |
| 68 | DependencyProperty.Register("IsLoading", |
| 69 | typeof(bool), |
| 70 | typeof(ChartsItemTypeList)); |
| 71 | |
| 72 | |
| 73 | #endregion |
| 74 | |
| 75 | #region IsShowBadge 是否显示徽章 |
| 76 | ///<summary> |
| 77 | /// |
| 78 | ///</summary> |
| 79 | public bool IsShowBadge |
| 80 | { |
| 81 | get { return (bool)GetValue(IsShowBadgeProperty); } |
| 82 | set { SetValue(IsShowBadgeProperty, value); } |
| 83 | } |
| 84 | public static readonly DependencyProperty IsShowBadgeProperty = |
| 85 | DependencyProperty.Register("IsShowBadge", |
| 86 | typeof(bool), |
| 87 | typeof(ChartsItemTypeList)); |
| 88 | |
| 89 | |
| 90 | #endregion |
| 91 | |
| 92 | #region 图标大小 |
| 93 | ///<summary> |
| 94 | /// |
| 95 | ///</summary> |
| 96 | public double IconSize |
| 97 | { |
| 98 | get { return (double)GetValue(IconSizeProperty); } |
| 99 | set { SetValue(IconSizeProperty, value); } |
| 100 | } |
| 101 | public static readonly DependencyProperty IconSizeProperty = |
| 102 | DependencyProperty.Register("IconSize", |
| 103 | typeof(double), |
| 104 | typeof(ChartsItemTypeList), new PropertyMetadata((double)25)); |
| 105 | |
| 106 | |
| 107 | #endregion |
| 108 | private TextBlock NameTextObj, ValueTextObj; |
| 109 | private Rectangle ValueBlockObj; |
| 110 | private StackPanel ValueContainer; |
| 111 | |
| 112 | private Image IconObj; |
| 113 | private bool isRendering = false; |
| 114 | private bool IsAddEvent = false; |
| 115 | public ChartsItemTypeList() |
| 116 | { |
| 117 | DefaultStyleKey = typeof(ChartsItemTypeList); |
| 118 | Unloaded += ChartsItemTypeList_Unloaded; |
| 119 | } |
| 120 | |
| 121 | private void ChartsItemTypeList_Unloaded(object sender, RoutedEventArgs e) |
| 122 | { |
| 123 | Unloaded -= ChartsItemTypeList_Unloaded; |
| 124 | Loaded -= ChartsItemTypeList_Loaded; |
| 125 | } |
| 126 | |
| 127 | public override void OnApplyTemplate() |
| 128 | { |
| 129 | base.OnApplyTemplate(); |
| 130 | NameTextObj = GetTemplateChild("NameTextObj") as TextBlock; |
| 131 | ValueTextObj = GetTemplateChild("ValueTextObj") as TextBlock; |
| 132 | ValueBlockObj = GetTemplateChild("ValueBlockObj") as Rectangle; |
| 133 | |
| 134 | ValueContainer = GetTemplateChild("ValueContainer") as StackPanel; |
| 135 | |
| 136 | IconObj = GetTemplateChild("IconObj") as Image; |
| 137 | |
| 138 | if (!IsAddEvent) |
| 139 | { |
| 140 | Loaded += ChartsItemTypeList_Loaded; |
| 141 | IsAddEvent = true; |
| 142 | } |
| 143 | |
| 144 | var parent = Parent as FrameworkElement; |
| 145 | parent.SizeChanged += Parent_SizeChanged; |
| 146 | } |
| 147 | |
| 148 | private void Parent_SizeChanged(object sender, SizeChangedEventArgs e) |
| 149 | { |
| 150 | UpdateValueBlockWidth(); |
| 151 | } |
| 152 | |
| 153 | private void ChartsItemTypeList_Loaded(object sender, RoutedEventArgs e) |
| 154 | { |
| 155 | Render(); |
| 156 | } |
| 157 | |
| 158 | private void Render() |
| 159 | { |
| 160 | if (isRendering || Data == null) |
| 161 | { |
| 162 | return; |
| 163 | } |
| 164 | isRendering = true; |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | ValueTextObj.Text = Data.Tag; |
| 174 | IconObj.Source = Imager.Load(Data.Icon); |
| 175 | |
| 176 | ValueTextObj.SizeChanged += (e, c) => |
| 177 | { |
| 178 | if (MaxValue <= 0) |
| 179 | { |
| 180 | return; |
| 181 | } |
| 182 | UpdateValueBlockWidth(); |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | public void UpdateValueBlockWidth() |
| 187 | { |
| 188 | if (Data == null || !IsLoaded) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | ValueBlockObj.Width = (Data.Value / MaxValue) * (ValueContainer.ActualWidth * 0.95 - ValueTextObj.ActualWidth); |
| 193 | } |
| 194 | |
| 195 | protected override void OnMouseEnter(MouseEventArgs e) |
| 196 | { |
| 197 | base.OnMouseEnter(e); |
| 198 | VisualStateManager.GoToState(this, "MouseOver", true); |
| 199 | } |
| 200 | protected override void OnMouseLeave(MouseEventArgs e) |
| 201 | { |
| 202 | base.OnMouseLeave(e); |
| 203 | VisualStateManager.GoToState(this, "NormalState", true); |
| 204 | } |
| 205 | } |
| 206 | } |