WPFUtils.cs 12 KB

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