Skip to content

Commit c043df6

Browse files
committed
Update Thermometer.cs
1 parent 4c7adc9 commit c043df6

File tree

1 file changed

+46
-95
lines changed

1 file changed

+46
-95
lines changed
Lines changed: 46 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,62 @@
1-
using System;
2-
using System.Windows;
3-
using System.Windows.Controls;
1+
using System.Windows;
42
using System.Windows.Media;
53

64
namespace WPFDevelopers.Controls
75
{
8-
public class Thermometer : Control
6+
public class Thermometer : ScaleBase
97
{
10-
public static readonly DependencyProperty MaxValueProperty =
11-
DependencyProperty.Register("MaxValue", typeof(double), typeof(Thermometer), new UIPropertyMetadata(40.0));
12-
13-
public static readonly DependencyProperty MinValueProperty =
14-
DependencyProperty.Register("MinValue", typeof(double), typeof(Thermometer), new UIPropertyMetadata(-10.0));
15-
16-
/// <summary>
17-
/// 当前值
18-
/// </summary>
19-
public static readonly DependencyProperty CurrentValueProperty =
20-
DependencyProperty.Register("CurrentValue", typeof(double), typeof(Thermometer),
21-
new UIPropertyMetadata(OnCurrentValueChanged));
22-
23-
/// <summary>
24-
/// 步长
25-
/// </summary>
26-
public static readonly DependencyProperty IntervalProperty =
27-
DependencyProperty.Register("Interval", typeof(double), typeof(Thermometer), new UIPropertyMetadata(10.0));
28-
29-
/// <summary>
30-
/// 当前值的图形坐标点
31-
/// </summary>
32-
public static readonly DependencyProperty CurrentGeometryProperty =
33-
DependencyProperty.Register("CurrentGeometry", typeof(Geometry), typeof(Thermometer), new PropertyMetadata(
34-
Geometry.Parse(@"M 2 132.8
35-
a 4 4 0 0 1 4 -4
36-
h 18
37-
a 4 4 0 0 1 4 4
38-
v 32.2
39-
a 4 4 0 0 1 -4 4
40-
h -18
41-
a 4 4 0 0 1 -4 -4 z")));
42-
43-
/// <summary>
44-
/// 构造函数
45-
/// </summary>
8+
public new double Maximum
9+
{
10+
get { return base.Maximum; }
11+
12+
}
13+
public new double Minimum
14+
{
15+
get { return base.Minimum; }
16+
17+
}
4618
static Thermometer()
4719
{
4820
DefaultStyleKeyProperty.OverrideMetadata(typeof(Thermometer),
4921
new FrameworkPropertyMetadata(typeof(Thermometer)));
5022
}
51-
52-
public double MaxValue
23+
public Thermometer()
5324
{
54-
get => (double)GetValue(MaxValueProperty);
55-
56-
set => SetValue(MaxValueProperty, value);
57-
}
58-
59-
public double MinValue
60-
{
61-
get => (double)GetValue(MinValueProperty);
62-
63-
set => SetValue(MinValueProperty, value);
25+
SetValue(MaximumProperty, 40.0);
26+
SetValue(MinimumProperty, -10.0);
27+
SetValue(IntervalProperty, 10.0);
28+
SetValue(GeometryProperty, Geometry.Parse(@"M 2 132.8
29+
a 4 4 0 0 1 4 -4
30+
h 18
31+
a 4 4 0 0 1 4 4
32+
v 32.2
33+
a 4 4 0 0 1 -4 4
34+
h -18
35+
a 4 4 0 0 1 -4 -4 z"));
6436
}
65-
66-
public double CurrentValue
37+
protected override void OnValueChanged(double oldValue, double newValue)
6738
{
68-
get => (double)GetValue(CurrentValueProperty);
69-
70-
set
71-
{
72-
SetValue(CurrentValueProperty, value);
73-
74-
PaintPath();
75-
}
39+
PaintPath();
7640
}
7741

78-
public double Interval
42+
protected override void OnMaximumChanged(double oldValue, double newValue)
7943
{
80-
get => (double)GetValue(IntervalProperty);
81-
82-
set => SetValue(IntervalProperty, value);
44+
InvalidateVisual();
8345
}
8446

85-
public Geometry CurrentGeometry
47+
protected override void OnMinimumChanged(double oldValue, double newValue)
8648
{
87-
get => (Geometry)GetValue(CurrentGeometryProperty);
88-
89-
set => SetValue(CurrentGeometryProperty, value);
49+
InvalidateVisual();
9050
}
9151

92-
private static void OnCurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
52+
protected override void OnIntervalChanged(double oldValue, double newValue)
9353
{
94-
var thermometer = d as Thermometer;
95-
thermometer.CurrentValue = Convert.ToDouble(e.NewValue);
54+
InvalidateVisual();
9655
}
9756

9857
public override void OnApplyTemplate()
9958
{
10059
base.OnApplyTemplate();
101-
10260
PaintPath();
10361
}
10462

@@ -168,22 +126,22 @@ protected override void OnRender(DrawingContext drawingContext)
168126

169127
#region 画刻度
170128

171-
var total_Value = MaxValue - MinValue;
129+
var total_Value = Maximum - Minimum;
172130

173131
var cnt = total_Value / Interval;
174132

175133
var one_value = 161d / cnt;
176134

177135
for (var i = 0; i <= cnt; i++)
178136
{
179-
var formattedText = DrawingContextHelper.GetFormattedText($"{MaxValue - i * Interval}",
137+
var formattedText = DrawingContextHelper.GetFormattedText($"{Maximum - i * Interval}",
180138
(Brush)DrawingContextHelper.BrushConverter.ConvertFromString("#82848A"), FlowDirection.LeftToRight,
181139
14D);
182140

183141
drawingContext.DrawText(formattedText,
184142
new Point(43, i * one_value - formattedText.Height / 2d)); //减去字体高度的一半
185143

186-
formattedText = DrawingContextHelper.GetFormattedText($"{(MaxValue - i * Interval) * 1.8d + 32d}",
144+
formattedText = DrawingContextHelper.GetFormattedText($"{(Maximum - i * Interval) * 1.8d + 32d}",
187145
(Brush)DrawingContextHelper.BrushConverter.ConvertFromString("#82848A"), textSize: 14D);
188146

189147
drawingContext.DrawText(formattedText, new Point(-13, i * one_value - formattedText.Height / 2d));
@@ -206,25 +164,18 @@ protected override void OnRender(DrawingContext drawingContext)
206164
/// </summary>
207165
private void PaintPath()
208166
{
209-
var one_value = 161d / ((MaxValue - MinValue) / Interval);
210-
167+
var one_value = 161d / ((Maximum - Minimum) / Interval);
211168
var width = 26d;
212-
213-
var height = 169d - (MaxValue - CurrentValue) * (one_value / Interval);
214-
215-
var x = 2d;
216-
217-
var y = 169d - (169d - (MaxValue - CurrentValue) * (one_value / Interval));
218-
219-
220-
CurrentGeometry = Geometry.Parse($@"M 2 {y + 4}
221-
a 4 4 0 0 1 4 -4
222-
h {width - 8}
223-
a 4 4 0 0 1 4 4
224-
v {height - 8}
225-
a 4 4 0 0 1 -4 4
226-
h -{width - 8}
227-
a 4 4 0 0 1 -4 -4 z");
169+
var height = 169d - (Maximum - Value) * (one_value / Interval);
170+
var y = 169d - (169d - (Maximum - Value) * (one_value / Interval));
171+
Geometry = Geometry.Parse($@"M 2 {y + 4}
172+
a 4 4 0 0 1 4 -4
173+
h {width - 8}
174+
a 4 4 0 0 1 4 4
175+
v {height - 8}
176+
a 4 4 0 0 1 -4 4
177+
h -{width - 8}
178+
a 4 4 0 0 1 -4 -4 z");
228179
}
229180
}
230181
}

0 commit comments

Comments
 (0)