|
1 | 1 | using Avalonia.Controls;
|
2 | 2 | using Avalonia.Controls.Primitives;
|
| 3 | +using Avalonia.Input; |
3 | 4 | using Avalonia.Interactivity;
|
4 | 5 |
|
5 |
| -namespace Counter |
| 6 | +namespace Counter; |
| 7 | + |
| 8 | +public static class ControlsExtensions |
6 | 9 | {
|
7 |
| - public static class ControlsExtensions |
| 10 | + public static T OnKeyDown<T>(this T element, params Action<T, KeyEventArgs>[] callbacks) where T : InputElement |
8 | 11 | {
|
9 |
| - public static Button OnClick(this Button button, params Action<Button, RoutedEventArgs>[] callbacks) |
| 12 | + for (int i = 0; i < callbacks.Length; i++) |
10 | 13 | {
|
11 |
| - for (int i = 0; i < callbacks.Length; i++) |
12 |
| - { |
13 |
| - int index = 0; |
14 |
| - button.Click += (obj, args) => callbacks[index]((Button)obj!, args); |
15 |
| - } |
16 |
| - |
17 |
| - return button; |
| 14 | + int index = 0; |
| 15 | + element.KeyDown += (obj, args) => callbacks[index]((T)obj!, args); |
18 | 16 | }
|
19 | 17 |
|
20 |
| - public static Button OnClick(this Button button, Action<Button, RoutedEventArgs> callback) |
| 18 | + return element; |
| 19 | + } |
| 20 | + |
| 21 | + public static T OnKeyDown<T>(this T element, Action<T, KeyEventArgs> callback) where T : InputElement |
| 22 | + { |
| 23 | + element.KeyDown += (obj, args) => callback((T)obj!, args); |
| 24 | + return element; |
| 25 | + } |
| 26 | + |
| 27 | + public static Button OnClick(this Button button, params Action<Button, RoutedEventArgs>[] callbacks) |
| 28 | + { |
| 29 | + for (int i = 0; i < callbacks.Length; i++) |
21 | 30 | {
|
22 |
| - button.Click += (obj, args) => callback((Button)obj!, args); |
23 |
| - return button; |
| 31 | + int index = 0; |
| 32 | + button.Click += (obj, args) => callbacks[index]((Button)obj!, args); |
24 | 33 | }
|
25 | 34 |
|
26 |
| - public static Button OnClick(this Button button, params Action[] callbacks) |
27 |
| - { |
28 |
| - for (int i = 0; i < callbacks.Length; i++) |
29 |
| - { |
30 |
| - int index = 0; |
31 |
| - button.Click += (obj, args) => callbacks[index](); |
32 |
| - } |
| 35 | + return button; |
| 36 | + } |
33 | 37 |
|
34 |
| - return button; |
35 |
| - } |
| 38 | + public static Button OnClick(this Button button, Action<Button, RoutedEventArgs> callback) |
| 39 | + { |
| 40 | + button.Click += (obj, args) => callback((Button)obj!, args); |
| 41 | + return button; |
| 42 | + } |
36 | 43 |
|
37 |
| - public static Button OnClick(this Button button, Action callback) |
| 44 | + public static Button OnClick(this Button button, params Action[] callbacks) |
| 45 | + { |
| 46 | + for (int i = 0; i < callbacks.Length; i++) |
38 | 47 | {
|
39 |
| - button.Click += (obj, args) => callback(); |
40 |
| - return button; |
| 48 | + int index = 0; |
| 49 | + button.Click += (obj, args) => callbacks[index](); |
41 | 50 | }
|
42 | 51 |
|
43 |
| - public static Panel AddChildren(this Panel control, params Control[] children) |
44 |
| - { |
45 |
| - control.Children.AddRange(children); |
46 |
| - return control; |
47 |
| - } |
| 52 | + return button; |
| 53 | + } |
48 | 54 |
|
49 |
| - public static ItemsControl AddItems(this ItemsControl control, params object[] items) |
50 |
| - { |
51 |
| - for (int i = 0; i < items.Length; i++) |
52 |
| - control.Items.Add(items[i]); |
53 |
| - return control; |
54 |
| - } |
| 55 | + public static Button OnClick(this Button button, Action callback) |
| 56 | + { |
| 57 | + button.Click += (obj, args) => callback(); |
| 58 | + return button; |
| 59 | + } |
55 | 60 |
|
56 |
| - public static MenuItem AddItems(this MenuItem menu, params MenuItem[] menuItems) |
57 |
| - { |
58 |
| - for (int i = 0; i < menuItems.Length; i++) |
59 |
| - menu.Items.Add(menuItems[i]); |
60 |
| - return menu; |
61 |
| - } |
| 61 | + public static Panel AddChildren(this Panel control, params Control[] children) |
| 62 | + { |
| 63 | + control.Children.AddRange(children); |
| 64 | + return control; |
| 65 | + } |
62 | 66 |
|
63 |
| - public static TabControl AddItems(this TabControl tabControl, TabItem[] items) |
64 |
| - { |
65 |
| - for (int i = 0; i < items.Length; i++) |
66 |
| - tabControl.Items.Add(items[i]); |
67 |
| - return tabControl; |
68 |
| - } |
| 67 | + public static ItemsControl AddItems(this ItemsControl control, params object[] items) |
| 68 | + { |
| 69 | + for (int i = 0; i < items.Length; i++) |
| 70 | + control.Items.Add(items[i]); |
| 71 | + return control; |
| 72 | + } |
69 | 73 |
|
70 |
| - public static T SetDock<T>(this T control, Dock dock) where T : Control |
71 |
| - { |
72 |
| - control.SetValue(DockPanel.DockProperty, dock); |
73 |
| - return control; |
74 |
| - } |
| 74 | + public static MenuItem AddItems(this MenuItem menu, params MenuItem[] menuItems) |
| 75 | + { |
| 76 | + for (int i = 0; i < menuItems.Length; i++) |
| 77 | + menu.Items.Add(menuItems[i]); |
| 78 | + return menu; |
| 79 | + } |
75 | 80 |
|
76 |
| - public static T SetGrid<T>(this T control, int column, int row, int columnSpan = 1, int rowSpan = 1) where T : Control |
77 |
| - { |
78 |
| - if (column != 0) |
79 |
| - SetColumn(control, column); |
80 |
| - if (row != 0) |
81 |
| - SetRow(control, row); |
82 |
| - if (columnSpan != 1) |
83 |
| - SetColumnSpan(control, columnSpan); |
84 |
| - if (rowSpan != 1) |
85 |
| - SetRowSpan(control, rowSpan); |
86 |
| - return control; |
87 |
| - } |
| 81 | + public static TabControl AddItems(this TabControl tabControl, TabItem[] items) |
| 82 | + { |
| 83 | + for (int i = 0; i < items.Length; i++) |
| 84 | + tabControl.Items.Add(items[i]); |
| 85 | + return tabControl; |
| 86 | + } |
88 | 87 |
|
89 |
| - public static T SetColumn<T>(this T control, int value) where T : Control |
90 |
| - { |
91 |
| - control.SetValue(Grid.ColumnProperty, value); |
92 |
| - return control; |
93 |
| - } |
| 88 | + public static T SetDock<T>(this T control, Dock dock) where T : Control |
| 89 | + { |
| 90 | + control.SetValue(DockPanel.DockProperty, dock); |
| 91 | + return control; |
| 92 | + } |
94 | 93 |
|
95 |
| - public static T SetRow<T>(this T control, int value) where T : Control |
96 |
| - { |
97 |
| - control.SetValue(Grid.RowProperty, value); |
98 |
| - return control; |
99 |
| - } |
| 94 | + public static T SetGrid<T>(this T control, int column, int row, int columnSpan = 1, int rowSpan = 1) where T : Control |
| 95 | + { |
| 96 | + if (column != 0) |
| 97 | + SetColumn(control, column); |
| 98 | + if (row != 0) |
| 99 | + SetRow(control, row); |
| 100 | + if (columnSpan != 1) |
| 101 | + SetColumnSpan(control, columnSpan); |
| 102 | + if (rowSpan != 1) |
| 103 | + SetRowSpan(control, rowSpan); |
| 104 | + return control; |
| 105 | + } |
100 | 106 |
|
101 |
| - public static T SetColumnSpan<T>(this T control, int value) where T : Control |
102 |
| - { |
103 |
| - control.SetValue(Grid.ColumnSpanProperty, value); |
104 |
| - return control; |
105 |
| - } |
| 107 | + public static T SetColumn<T>(this T control, int value) where T : Control |
| 108 | + { |
| 109 | + control.SetValue(Grid.ColumnProperty, value); |
| 110 | + return control; |
| 111 | + } |
106 | 112 |
|
107 |
| - public static T SetRowSpan<T>(this T control, int value) where T : Control |
108 |
| - { |
109 |
| - control.SetValue(Grid.RowSpanProperty, value); |
110 |
| - return control; |
111 |
| - } |
| 113 | + public static T SetRow<T>(this T control, int value) where T : Control |
| 114 | + { |
| 115 | + control.SetValue(Grid.RowProperty, value); |
| 116 | + return control; |
| 117 | + } |
112 | 118 |
|
113 |
| - public static T SetTooltip<T>(this T control, string tooltip) where T : Control |
114 |
| - { |
115 |
| - control.SetValue(ToolTip.TipProperty, tooltip); |
116 |
| - return control; |
117 |
| - } |
| 119 | + public static T SetColumnSpan<T>(this T control, int value) where T : Control |
| 120 | + { |
| 121 | + control.SetValue(Grid.ColumnSpanProperty, value); |
| 122 | + return control; |
| 123 | + } |
118 | 124 |
|
119 |
| - public static T OnSelectionChanged<T>(this T control, Action action) where T : SelectingItemsControl |
120 |
| - { |
121 |
| - control.SelectionChanged += (_, _) => action(); |
122 |
| - return control; |
123 |
| - } |
| 125 | + public static T SetRowSpan<T>(this T control, int value) where T : Control |
| 126 | + { |
| 127 | + control.SetValue(Grid.RowSpanProperty, value); |
| 128 | + return control; |
| 129 | + } |
| 130 | + |
| 131 | + public static T SetTooltip<T>(this T control, string tooltip) where T : Control |
| 132 | + { |
| 133 | + control.SetValue(ToolTip.TipProperty, tooltip); |
| 134 | + return control; |
| 135 | + } |
| 136 | + |
| 137 | + public static T OnSelectionChanged<T>(this T control, Action action) where T : SelectingItemsControl |
| 138 | + { |
| 139 | + control.SelectionChanged += (_, _) => action(); |
| 140 | + return control; |
| 141 | + } |
| 142 | + |
| 143 | + public static T OnTextChanged<T>(this T control, Action action) where T : Control |
| 144 | + { |
| 145 | + control.SizeChanged += (_, e) => action(); |
| 146 | + return control; |
124 | 147 | }
|
125 | 148 | }
|
0 commit comments