| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading.Tasks; |
| 6 | using System.Windows; |
| 7 | using System.Windows.Controls; |
| 8 | using System.Windows.Input; |
| 9 | using System.Windows.Shapes; |
| 10 | using UI.Base.Color; |
| 11 | |
| 12 | namespace UI.Controls.Charts |
| 13 | { |
| 14 | public class ChartItemTypeColumn : Control |
| 15 | { |
| 16 | #region MaxValue 最大值 |
| 17 | ///<summary> |
| 18 | /// |
| 19 | ///</summary> |
| 20 | public double MaxValue |
| 21 | { |
| 22 | get { return (double)GetValue(MaxValueProperty); } |
| 23 | set { SetValue(MaxValueProperty, value); } |
| 24 | } |
| 25 | public static readonly DependencyProperty MaxValueProperty = |
| 26 | DependencyProperty.Register("MaxValue", |
| 27 | typeof(double), |
| 28 | typeof(ChartItemTypeColumn)); |
| 29 | |
| 30 | |
| 31 | #endregion |
| 32 | |
| 33 | #region 值 |
| 34 | ///<summary> |
| 35 | /// |
| 36 | ///</summary> |
| 37 | public double Value |
| 38 | { |
| 39 | get { return (double)GetValue(ValueProperty); } |
| 40 | set { SetValue(ValueProperty, value); } |
| 41 | } |
| 42 | public static readonly DependencyProperty ValueProperty = |
| 43 | DependencyProperty.Register("Value", |
| 44 | typeof(double), |
| 45 | typeof(ChartItemTypeColumn)); |
| 46 | |
| 47 | |
| 48 | #endregion |
| 49 | #region 颜色 |
| 50 | ///<summary> |
| 51 | /// |
| 52 | ///</summary> |
| 53 | public string Color |
| 54 | { |
| 55 | get { return (string)GetValue(ColorProperty); } |
| 56 | set { SetValue(ColorProperty, value); } |
| 57 | } |
| 58 | public static readonly DependencyProperty ColorProperty = |
| 59 | DependencyProperty.Register("Color", |
| 60 | typeof(string), |
| 61 | typeof(ChartItemTypeColumn)); |
| 62 | |
| 63 | |
| 64 | #endregion |
| 65 | #region 列名 |
| 66 | ///<summary> |
| 67 | /// |
| 68 | ///</summary> |
| 69 | public string ColumnName |
| 70 | { |
| 71 | get { return (string)GetValue(ColumnNameProperty); } |
| 72 | set { SetValue(ColumnNameProperty, value); } |
| 73 | } |
| 74 | public static readonly DependencyProperty ColumnNameProperty = |
| 75 | DependencyProperty.Register("ColumnName", |
| 76 | typeof(string), |
| 77 | typeof(ChartItemTypeColumn)); |
| 78 | |
| 79 | |
| 80 | #endregion |
| 81 | |
| 82 | private bool isRendering = false; |
| 83 | private Rectangle ValueBlockObj; |
| 84 | private Border ValueContainer; |
| 85 | private bool IsAddEvent = false; |
| 86 | public ChartItemTypeColumn() |
| 87 | { |
| 88 | DefaultStyleKey = typeof(ChartItemTypeColumn); |
| 89 | Unloaded += ChartItemTypeColumn_Unloaded; |
| 90 | } |
| 91 | |
| 92 | public override void OnApplyTemplate() |
| 93 | { |
| 94 | base.OnApplyTemplate(); |
| 95 | ValueBlockObj = GetTemplateChild("ValueBlockObj") as Rectangle; |
| 96 | ValueContainer = GetTemplateChild("ValueContainer") as Border; |
| 97 | if (!IsAddEvent) |
| 98 | { |
| 99 | Loaded += ChartItemTypeColumn_Loaded; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private void ChartItemTypeColumn_Unloaded(object sender, RoutedEventArgs e) |
| 104 | { |
| 105 | Loaded -= ChartItemTypeColumn_Loaded; |
| 106 | Unloaded -= ChartItemTypeColumn_Unloaded; |
| 107 | } |
| 108 | |
| 109 | private void ChartItemTypeColumn_Loaded(object sender, RoutedEventArgs e) |
| 110 | { |
| 111 | Render(); |
| 112 | IsAddEvent = true; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | private void Render() |
| 117 | { |
| 118 | if (isRendering) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (!string.IsNullOrEmpty(Color)) |
| 124 | { |
| 125 | ValueBlockObj.Fill = Colors.GetFromString(Color); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | Update(); |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | } |
| 139 | |
| 140 | public void Update() |
| 141 | { |
| 142 | |
| 143 | ValueBlockObj.Height = (Value / MaxValue) * (ValueContainer.ActualHeight); |
| 144 | } |
| 145 | |
| 146 | protected override void OnMouseEnter(MouseEventArgs e) |
| 147 | { |
| 148 | base.OnMouseEnter(e); |
| 149 | VisualStateManager.GoToState(this, "MouseOver", true); |
| 150 | } |
| 151 | protected override void OnMouseLeave(MouseEventArgs e) |
| 152 | { |
| 153 | base.OnMouseLeave(e); |
| 154 | VisualStateManager.GoToState(this, "NormalState", true); |
| 155 | } |
| 156 | } |
| 157 | } |