WPFUtils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq.Expressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. using InABox.Core;
  10. using Image = System.Windows.Controls.Image;
  11. namespace InABox.WPF;
  12. public class FuncTemplateSelector : DataTemplateSelector
  13. {
  14. public Func<object, DependencyObject, FrameworkElement?> TemplateFunc { get; set; }
  15. public FuncTemplateSelector(Func<object, DependencyObject, FrameworkElement?> templateFunc)
  16. {
  17. TemplateFunc = templateFunc;
  18. }
  19. public override DataTemplate SelectTemplate(object item, DependencyObject container)
  20. {
  21. return TemplateGenerator.CreateDataTemplate(() =>
  22. {
  23. return TemplateFunc(item, container);
  24. });
  25. }
  26. }
  27. public static class WPFUtils
  28. {
  29. public static void Bind<T, TProperty>(
  30. this FrameworkElement element,
  31. DependencyProperty property,
  32. T source,
  33. Expression<Func<T, TProperty>> expression,
  34. IValueConverter? converter = null,
  35. string? format = null)
  36. {
  37. element.SetBinding(
  38. property,
  39. new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
  40. {
  41. Source = source,
  42. Converter = converter,
  43. StringFormat = format
  44. }
  45. );
  46. }
  47. public static void Bind<T, TProperty>(
  48. this FrameworkElement element,
  49. DependencyProperty property,
  50. Expression<Func<T, TProperty>> expression,
  51. IValueConverter? converter = null,
  52. BindingMode mode = BindingMode.Default,
  53. string? format = null )
  54. {
  55. element.SetBinding(
  56. property,
  57. new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
  58. {
  59. Converter = converter,
  60. StringFormat = format,
  61. Mode = mode
  62. }
  63. );
  64. }
  65. public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
  66. where T : DependencyObject
  67. {
  68. DependencyObject? parent = dependencyObject;
  69. do
  70. {
  71. parent = LogicalTreeHelper.GetParent(parent);
  72. } while(parent != null && parent is not T);
  73. return parent as T;
  74. }
  75. public static int GetRow(this Grid grid, DependencyObject dependencyObject)
  76. {
  77. while (true)
  78. {
  79. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  80. if (parent == null)
  81. return -1;
  82. if (parent == grid)
  83. return Grid.GetRow(dependencyObject as UIElement);
  84. dependencyObject = parent;
  85. }
  86. }
  87. public static int GetRowSpan(this Grid grid, DependencyObject dependencyObject)
  88. {
  89. while (true)
  90. {
  91. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  92. if (parent == null)
  93. return -1;
  94. if (parent == grid)
  95. return Grid.GetRowSpan(dependencyObject as UIElement);
  96. dependencyObject = parent;
  97. }
  98. }
  99. public static int GetColumn(this Grid grid, DependencyObject dependencyObject)
  100. {
  101. while (true)
  102. {
  103. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  104. if (parent == null)
  105. return -1;
  106. if (parent == grid)
  107. return Grid.GetColumn(dependencyObject as UIElement);
  108. dependencyObject = parent;
  109. }
  110. }
  111. public static int GetColumnSpan(this Grid grid, DependencyObject dependencyObject)
  112. {
  113. while (true)
  114. {
  115. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  116. if (parent == null)
  117. return -1;
  118. if (parent == grid)
  119. return Grid.GetColumnSpan(dependencyObject as UIElement);
  120. dependencyObject = parent;
  121. }
  122. }
  123. public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan = 1, int colspan = 1)
  124. {
  125. element.SetValue(Grid.ColumnProperty, column);
  126. element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
  127. element.SetValue(Grid.RowProperty, row);
  128. element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
  129. }
  130. public static Grid AddChild(this Grid grid, FrameworkElement element, int row, int column, int rowSpan = 1, int colSpan = 1)
  131. {
  132. element.SetGridPosition(row, column, rowSpan, colSpan);
  133. grid.Children.Add(element);
  134. return grid;
  135. }
  136. public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
  137. {
  138. if (depObj != null)
  139. //ContentControl cc = depObj as ContentControl;
  140. //if (cc != null)
  141. //{
  142. // if (cc.Content == null)
  143. // yield return null;
  144. // if (cc.Content is T)
  145. // yield return cc.Content as T;
  146. // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
  147. // yield return child;
  148. //}
  149. //else
  150. for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  151. {
  152. var child = VisualTreeHelper.GetChild(depObj, i);
  153. if (child is null)
  154. continue;
  155. if (child is T t)
  156. yield return t;
  157. foreach (var childOfChild in FindVisualChildren<T>(child))
  158. yield return childOfChild;
  159. }
  160. }
  161. #region Grid Columns + Rows
  162. public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)
  163. {
  164. var colDef = new ColumnDefinition { Width = new GridLength(value, type) };
  165. grid.ColumnDefinitions.Add(colDef);
  166. return colDef;
  167. }
  168. public static ColumnDefinition AddColumn(this Grid grid, double value)
  169. {
  170. var colDef = new ColumnDefinition { Width = new GridLength(value) };
  171. grid.ColumnDefinitions.Add(colDef);
  172. return colDef;
  173. }
  174. public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)
  175. {
  176. var rowDef = new RowDefinition { Height = new GridLength(value, type) };
  177. grid.RowDefinitions.Add(rowDef);
  178. return rowDef;
  179. }
  180. public static RowDefinition AddRow(this Grid grid, double value)
  181. {
  182. var rowDef = new RowDefinition { Height = new GridLength(value) };
  183. grid.RowDefinitions.Add(rowDef);
  184. return rowDef;
  185. }
  186. #endregion
  187. #region Menu Utils
  188. private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
  189. {
  190. if (index != -1)
  191. {
  192. menu.Items.Insert(index, item);
  193. }
  194. else
  195. {
  196. menu.Items.Add(item);
  197. }
  198. }
  199. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
  200. {
  201. var item = new MenuItem { Header = caption, IsEnabled = enabled };
  202. if (image != null)
  203. item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
  204. ItemsControlInsert(menu, item, index);
  205. return item;
  206. }
  207. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
  208. {
  209. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  210. if (click != null)
  211. {
  212. item.Click += (o, e) =>
  213. {
  214. click();
  215. };
  216. }
  217. return item;
  218. }
  219. private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
  220. {
  221. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  222. item.Tag = tag;
  223. item.Click += (o, e) =>
  224. {
  225. click((T)(o as MenuItem)!.Tag);
  226. };
  227. return item;
  228. }
  229. public delegate void CheckToggleAction(bool isChecked);
  230. public delegate void CheckToggleAction<T>(T tag, bool isChecked);
  231. private static MenuItem DoAddCheckItem(ItemsControl menu, string caption, CheckToggleAction click, bool isChecked, bool enabled, int index = -1)
  232. {
  233. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  234. item.Click += (o, e) =>
  235. {
  236. click(item.IsChecked);
  237. };
  238. ItemsControlInsert(menu, item, index);
  239. return item;
  240. }
  241. private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
  242. {
  243. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  244. item.Tag = tag;
  245. item.Click += (o, e) =>
  246. {
  247. click((T)(o as MenuItem)!.Tag, item.IsChecked);
  248. };
  249. ItemsControlInsert(menu, item, index);
  250. return item;
  251. }
  252. private static Separator DoAddSeparator(ItemsControl menu, int index)
  253. {
  254. var separator = new Separator();
  255. ItemsControlInsert(menu, separator, index);
  256. return separator;
  257. }
  258. private static Separator? DoAddSeparatorIfNeeded(ItemsControl menu, int index)
  259. {
  260. if (menu.Items.Count == 0) return null;
  261. var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
  262. if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
  263. var last = menu.Items[lastIndex];
  264. if (last is Separator) return null;
  265. var separator = new Separator();
  266. ItemsControlInsert(menu, separator, index);
  267. return separator;
  268. }
  269. private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
  270. {
  271. while(menu.Items.Count > 0 && menu.Items[0] is Separator)
  272. {
  273. menu.Items.RemoveAt(0);
  274. }
  275. while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
  276. {
  277. menu.Items.RemoveAt(menu.Items.Count - 1);
  278. }
  279. }
  280. public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
  281. public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
  282. public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  283. public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  284. public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
  285. public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
  286. public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  287. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  288. public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  289. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  290. public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  291. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  292. public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  293. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  294. public static MenuItem AddCheckItem(this ContextMenu menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
  295. => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
  296. public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
  297. => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
  298. #endregion
  299. }