-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlsH.cs
176 lines (158 loc) · 5.79 KB
/
ControlsH.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
public class ControlsH {
public static DependencyObject GetScrollViewer(DependencyObject o) {
if (o is ScrollViewer) { return o; }
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++) {
var child = VisualTreeHelper.GetChild(o, i);
var result = GetScrollViewer(child);
if (result == null) {
continue;
} else {
return result;
}
}
return null;
}
public static T GetFirstParentOfType<T>(DependencyObject @object) {
int i = 0;
while (true) {
i++;
var parent = VisualTreeHelper.GetParent(@object);
if (parent == null) {
throw new Exception($"Parent of the given type not found. The loop ran {i} times.");
}
if (parent is T t) {
return t;
}
}
}
public static void EnsureThickness(Shape shape) {
shape.SnapsToDevicePixels = true;
shape.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
}
public static void EnsureThickness(UIElement element) {
element.SnapsToDevicePixels = true;
element.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
}
public static void BehaveText(TextBox box, string text) {
box.Text = text;
box.Foreground = Brushes.Gray;
box.GotKeyboardFocus += (sender, e) => {
if (box.Text == text) {
box.Text = "";
box.Foreground = Brushes.Black;
}
};
box.LostKeyboardFocus += (sender, e) => {
if (box.Text == "" || box.Text == text) {
box.Text = text;
box.Foreground = Brushes.Gray;
}
};
}
public static void BehaveText(PasswordBox box, string text) {
box.Password = text;
box.Foreground = Brushes.Gray;
box.GotKeyboardFocus += (sender, e) => {
if (box.Password == text) {
box.Password = "";
box.Foreground = Brushes.Black;
}
};
box.LostKeyboardFocus += (sender, e) => {
if (box.Password == "" || box.Password == text) {
box.Password = text;
box.Foreground = Brushes.Gray;
}
};
}
public static void CreateColumns(DataGrid dataGrid, object @class) {
CreateColumns(dataGrid, @class.GetType());
}
public static void CreateColumns(DataGrid dataGrid, Type type) {
var ps = type.GetProperties();
for (int i = 0; i < ps.Length; i++) {
var p = ps[i];
CreateColumn(dataGrid, p.Name, 1, DataGridLengthUnitType.Auto);
}
}
public static void CreateColumns(DataGrid dataGrid, params string[] headers) {
for (int i = 0; i < headers.Length; i++) {
CreateColumn(dataGrid, headers[i], 1, DataGridLengthUnitType.Auto);
}
}
public static void CreateColumn(DataGrid datagrid, string header, int width = 1, DataGridLengthUnitType type = DataGridLengthUnitType.Star) {
var c = new DataGridTextColumn() {
Header = header,
Binding = new Binding(header),
Width = new DataGridLength(width, type)
};
datagrid.Columns.Add(c);
}
public static void CreateColumn(DataGrid datagrid, string header, string binding = null, int width = 1, DataGridLengthUnitType type = DataGridLengthUnitType.Star) {
var c = new DataGridTextColumn() {
Header = header,
Binding = new Binding(binding ?? header),
Width = new DataGridLength(width, type)
};
datagrid.Columns.Add(c);
}
public static void ApplyFontWeight(TextRange textRange, FontWeight weight) {
var cw = FontWeights.Normal;
try {
cw = (FontWeight)textRange.GetPropertyValue(TextElement.FontWeightProperty);
} catch (Exception) { }
if (cw == weight) {
weight = FontWeights.Normal;
} else {
weight = FontWeights.Bold;
}
textRange.ApplyPropertyValue(TextElement.FontWeightProperty, weight);
}
public static List<T> GetChildrenOfType<T> (UIElement element) {
var list = new List<T>();
if (element is Panel panel) {
foreach (UIElement child in panel.Children) {
list.AddRange(GetChildrenOfType<T>(child));
}
}
if (element is T t) {
list.Add(t);
}
return list;
}
/*public static Rect GetRect(FrameworkElement element) {
double x, y, width, height;
x = y = width = height = 0;
var parent = element.Parent;
if (parent is Grid grid) {
switch (element.HorizontalAlignment) {
case HorizontalAlignment.Left:
x = element.Margin.Left;
width = element.Width;
break;
case HorizontalAlignment.Stretch:
width = grid.ActualWidth - (element.Margin.Left + element.Margin.Right);
x = width - element.Margin.Right;
break;
case HorizontalAlignment.Right:
width = element.Width;
x = grid.ActualWidth - (element.Margin.Right + width);
break;
case HorizontalAlignment.Center:
break;
default: break;
}
switch (element.VerticalAlignment) {
}
}
return new Rect(x, y, width, height);
}*/
}