| 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.Effects; |
| 16 | using System.Windows.Media.Imaging; |
| 17 | using System.Windows.Navigation; |
| 18 | using System.Windows.Shapes; |
| 19 | using UI.Controls.Charts.Model; |
| 20 | |
| 21 | namespace UI.Controls.Charts |
| 22 | { |
| 23 | public class ChartsItemTypeCard : Control |
| 24 | { |
| 25 | #region ChartsType 样式类型 |
| 26 | ///<summary> |
| 27 | /// |
| 28 | ///</summary> |
| 29 | public ChartsDataModel Data |
| 30 | { |
| 31 | get { return (ChartsDataModel)GetValue(DataProperty); } |
| 32 | set { SetValue(DataProperty, value); } |
| 33 | } |
| 34 | public static readonly DependencyProperty DataProperty = |
| 35 | DependencyProperty.Register("Data", |
| 36 | typeof(ChartsDataModel), |
| 37 | typeof(ChartsItemTypeCard)); |
| 38 | |
| 39 | |
| 40 | #endregion |
| 41 | |
| 42 | #region MaxValue 最大值 |
| 43 | ///<summary> |
| 44 | /// |
| 45 | ///</summary> |
| 46 | public double MaxValue |
| 47 | { |
| 48 | get { return (double)GetValue(MaxValueProperty); } |
| 49 | set { SetValue(MaxValueProperty, value); } |
| 50 | } |
| 51 | public static readonly DependencyProperty MaxValueProperty = |
| 52 | DependencyProperty.Register("MaxValue", |
| 53 | typeof(double), |
| 54 | typeof(ChartsItemTypeCard)); |
| 55 | |
| 56 | |
| 57 | #endregion |
| 58 | |
| 59 | #region IsLoading 是否正在加载中 |
| 60 | ///<summary> |
| 61 | /// |
| 62 | ///</summary> |
| 63 | public bool IsLoading |
| 64 | { |
| 65 | get { return (bool)GetValue(IsLoadingProperty); } |
| 66 | set { SetValue(IsLoadingProperty, value); } |
| 67 | } |
| 68 | public static readonly DependencyProperty IsLoadingProperty = |
| 69 | DependencyProperty.Register("IsLoading", |
| 70 | typeof(bool), |
| 71 | typeof(ChartsItemTypeCard)); |
| 72 | |
| 73 | |
| 74 | #endregion |
| 75 | private TextBlock NameTextObj, ValueTextObj; |
| 76 | private Rectangle ValueBlockObj; |
| 77 | private StackPanel ValueContainer; |
| 78 | private Image IconObj; |
| 79 | private bool isRendering = false; |
| 80 | private bool IsAddEvent = false; |
| 81 | public ChartsItemTypeCard() |
| 82 | { |
| 83 | DefaultStyleKey = typeof(ChartsItemTypeCard); |
| 84 | Unloaded += ChartsItemTypeCard_Unloaded; |
| 85 | } |
| 86 | |
| 87 | private void ChartsItemTypeCard_Unloaded(object sender, RoutedEventArgs e) |
| 88 | { |
| 89 | Unloaded -= ChartsItemTypeCard_Unloaded; |
| 90 | Loaded -= ChartsItemTypeCard_Loaded; |
| 91 | } |
| 92 | |
| 93 | public override void OnApplyTemplate() |
| 94 | { |
| 95 | base.OnApplyTemplate(); |
| 96 | NameTextObj = GetTemplateChild("NameTextObj") as TextBlock; |
| 97 | ValueTextObj = GetTemplateChild("ValueTextObj") as TextBlock; |
| 98 | ValueBlockObj = GetTemplateChild("ValueBlockObj") as Rectangle; |
| 99 | ValueContainer = GetTemplateChild("ValueContainer") as StackPanel; |
| 100 | IconObj = GetTemplateChild("IconObj") as Image; |
| 101 | |
| 102 | if (!IsAddEvent) |
| 103 | { |
| 104 | Loaded += ChartsItemTypeCard_Loaded; |
| 105 | IsAddEvent = true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private void ChartsItemTypeCard_Loaded(object sender, RoutedEventArgs e) |
| 110 | { |
| 111 | Render(); |
| 112 | } |
| 113 | |
| 114 | private void Render() |
| 115 | { |
| 116 | if (isRendering || Data == null) |
| 117 | { |
| 118 | return; |
| 119 | } |
| 120 | isRendering = true; |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | NameTextObj.SizeChanged += (e, c) => |
| 131 | { |
| 132 | |
| 133 | if (NameTextObj.ActualWidth > 121 && NameTextObj.FontSize > 8) |
| 134 | { |
| 135 | NameTextObj.FontSize = NameTextObj.FontSize - 1; |
| 136 | } |
| 137 | }; |
| 138 | ValueTextObj.Text = Data.Tag; |
| 139 | IconObj.Source = Imager.Load(Data.Icon); |
| 140 | |
| 141 | ValueTextObj.SizeChanged += (e, c) => |
| 142 | { |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | double size = (Data.Value / MaxValue) * ActualWidth / 3; |
| 148 | ValueBlockObj.Width = ValueBlockObj.Height = size; |
| 149 | |
| 150 | ValueBlockObj.Effect = new BlurEffect() |
| 151 | { |
| 152 | Radius = size, |
| 153 | RenderingBias = RenderingBias.Performance |
| 154 | }; |
| 155 | |
| 156 | }; |
| 157 | |
| 158 | } |
| 159 | |
| 160 | protected override void OnMouseEnter(MouseEventArgs e) |
| 161 | { |
| 162 | base.OnMouseEnter(e); |
| 163 | VisualStateManager.GoToState(this, "MouseOver", true); |
| 164 | } |
| 165 | protected override void OnMouseLeave(MouseEventArgs e) |
| 166 | { |
| 167 | base.OnMouseLeave(e); |
| 168 | VisualStateManager.GoToState(this, "NormalState", true); |
| 169 | } |
| 170 | } |
| 171 | } |