跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

DiffValue.cs

110 行 · 3.4 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Controls;
8
9namespace UI.Controls.Base
10{
11 public class DiffValue : StackPanel
12 {
13 public enum DiffType
14 {
15 /// <summary>
16 /// 百分比
17 /// </summary>
18 Percent,
19 /// <summary>
20 /// 数字
21 /// </summary>
22 Number
23 }
24 public double Value
25 {
26 get { return (double)GetValue(ValueProperty); }
27 set { SetValue(ValueProperty, value); }
28 }
29 public static readonly DependencyProperty ValueProperty =
30 DependencyProperty.Register("Value",
31 typeof(double),
32 typeof(DiffValue), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
33 public double LastValue
34 {
35 get { return (double)GetValue(LastValueProperty); }
36 set { SetValue(LastValueProperty, value); }
37 }
38 public static readonly DependencyProperty LastValueProperty =
39 DependencyProperty.Register("LastValue",
40 typeof(double),
41 typeof(DiffValue), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
42 public DiffType Type
43 {
44 get { return (DiffType)GetValue(TypeProperty); }
45 set { SetValue(TypeProperty, value); }
46 }
47 public static readonly DependencyProperty TypeProperty =
48 DependencyProperty.Register("Type",
49 typeof(DiffType),
50 typeof(DiffValue), new PropertyMetadata(DiffType.Percent, new PropertyChangedCallback(OnPropertyChanged)));
51
52 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
53 {
54 var control = d as DiffValue;
55 control.Render();
56 }
57
58 public DiffValue()
59 {
60 DefaultStyleKey = typeof(DiffValue);
61 Orientation = Orientation.Horizontal;
62 Render();
63 }
64
65
66
67 private void Render()
68 {
69 Children.Clear();
70
71 double diffValue = Value - LastValue;
72 double result = Type == DiffType.Percent ? diffValue / LastValue * 100 : diffValue;
73 if (Value > 0 && LastValue <= 0)
74 {
75 result = Type == DiffType.Percent ? 100 : Value;
76 }
77 else if (Value == LastValue)
78 {
79 result = 0;
80 }
81 var text = new TextBlock();
82 text.Text = Type == DiffType.Percent ? Math.Abs(result).ToString("f2") + "%" : Math.Abs(result).ToString();
83 text.VerticalAlignment = VerticalAlignment.Center;
84
85 var icon = new Icon();
86 icon.FontSize = 10;
87 icon.VerticalAlignment = VerticalAlignment.Center;
88
89 if (result > 0)
90 {
91 icon.IconType = IconTypes.ArrowUp8;
92 Children.Add(icon);
93 Children.Add(text);
94 }
95 else if (result < 0)
96 {
97 icon.IconType = IconTypes.ArrowDown8;
98 Children.Add(icon);
99 Children.Add(text);
100 }
101 else
102 {
103 icon.IconType = IconTypes.SubtractBold;
104 Children.Add(icon);
105 }
106
107
108 }
109 }
110}