|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Controls.Primitives; |
| 3 | +using Avalonia.Interactivity; |
| 4 | + |
| 5 | +namespace Counter |
| 6 | +{ |
| 7 | + public static class ControlsExtensions |
| 8 | + { |
| 9 | + public static Button OnClick(this Button button, params Action<Button, RoutedEventArgs>[] callbacks) |
| 10 | + { |
| 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; |
| 18 | + } |
| 19 | + |
| 20 | + public static Button OnClick(this Button button, Action<Button, RoutedEventArgs> callback) |
| 21 | + { |
| 22 | + button.Click += (obj, args) => callback((Button)obj!, args); |
| 23 | + return button; |
| 24 | + } |
| 25 | + |
| 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 | + } |
| 33 | + |
| 34 | + return button; |
| 35 | + } |
| 36 | + |
| 37 | + public static Button OnClick(this Button button, Action callback) |
| 38 | + { |
| 39 | + button.Click += (obj, args) => callback(); |
| 40 | + return button; |
| 41 | + } |
| 42 | + |
| 43 | + public static Panel AddChildren(this Panel control, params Control[] children) |
| 44 | + { |
| 45 | + control.Children.AddRange(children); |
| 46 | + return control; |
| 47 | + } |
| 48 | + |
| 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 | + |
| 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 | + } |
| 62 | + |
| 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 | + } |
| 69 | + |
| 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 | + } |
| 75 | + |
| 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 | + } |
| 88 | + |
| 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 | + } |
| 94 | + |
| 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 | + } |
| 100 | + |
| 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 | + } |
| 106 | + |
| 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 | + } |
| 112 | + |
| 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 | + } |
| 118 | + |
| 119 | + public static T OnSelectionChanged<T>(this T control, Action action) where T : SelectingItemsControl |
| 120 | + { |
| 121 | + control.SelectionChanged += (_, _) => action(); |
| 122 | + return control; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments