WPFUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using FontStyle = System.Windows.FontStyle;
  10. using Image = System.Windows.Controls.Image;
  11. namespace InABox.WPF
  12. {
  13. public static class WPFUtils
  14. {
  15. public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
  16. where T : DependencyObject
  17. {
  18. DependencyObject? parent = dependencyObject;
  19. do
  20. {
  21. parent = LogicalTreeHelper.GetParent(parent);
  22. } while(parent != null && parent is not T);
  23. return parent as T;
  24. }
  25. public static int GetRow(this Grid grid, DependencyObject dependencyObject)
  26. {
  27. while (true)
  28. {
  29. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  30. if (parent == null)
  31. return -1;
  32. if (parent == grid)
  33. return Grid.GetRow(dependencyObject as UIElement);
  34. dependencyObject = parent;
  35. }
  36. }
  37. public static int GetRowSpan(this Grid grid, DependencyObject dependencyObject)
  38. {
  39. while (true)
  40. {
  41. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  42. if (parent == null)
  43. return -1;
  44. if (parent == grid)
  45. return Grid.GetRowSpan(dependencyObject as UIElement);
  46. dependencyObject = parent;
  47. }
  48. }
  49. public static int GetColumn(this Grid grid, DependencyObject dependencyObject)
  50. {
  51. while (true)
  52. {
  53. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  54. if (parent == null)
  55. return -1;
  56. if (parent == grid)
  57. return Grid.GetColumn(dependencyObject as UIElement);
  58. dependencyObject = parent;
  59. }
  60. }
  61. public static int GetColumnSpan(this Grid grid, DependencyObject dependencyObject)
  62. {
  63. while (true)
  64. {
  65. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  66. if (parent == null)
  67. return -1;
  68. if (parent == grid)
  69. return Grid.GetColumnSpan(dependencyObject as UIElement);
  70. dependencyObject = parent;
  71. }
  72. }
  73. public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan = 1, int colspan = 1)
  74. {
  75. element.SetValue(Grid.ColumnProperty, column);
  76. element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
  77. element.SetValue(Grid.RowProperty, row);
  78. element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
  79. }
  80. public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
  81. {
  82. if (depObj != null)
  83. //ContentControl cc = depObj as ContentControl;
  84. //if (cc != null)
  85. //{
  86. // if (cc.Content == null)
  87. // yield return null;
  88. // if (cc.Content is T)
  89. // yield return cc.Content as T;
  90. // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
  91. // yield return child;
  92. //}
  93. //else
  94. for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  95. {
  96. var child = VisualTreeHelper.GetChild(depObj, i);
  97. if (child is null)
  98. continue;
  99. if (child is T t)
  100. yield return t;
  101. foreach (var childOfChild in FindVisualChildren<T>(child))
  102. yield return childOfChild;
  103. }
  104. }
  105. #region Menu Utils
  106. private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
  107. {
  108. if (index != -1)
  109. {
  110. menu.Items.Insert(index, item);
  111. }
  112. else
  113. {
  114. menu.Items.Add(item);
  115. }
  116. }
  117. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
  118. {
  119. var item = new MenuItem { Header = caption, IsEnabled = enabled };
  120. if (image != null)
  121. item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
  122. ItemsControlInsert(menu, item, index);
  123. return item;
  124. }
  125. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
  126. {
  127. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  128. if (click != null)
  129. {
  130. item.Click += (o, e) =>
  131. {
  132. click();
  133. };
  134. }
  135. return item;
  136. }
  137. private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
  138. {
  139. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  140. item.Tag = tag;
  141. item.Click += (o, e) =>
  142. {
  143. click((T)(o as MenuItem)!.Tag);
  144. };
  145. return item;
  146. }
  147. public delegate void CheckToggleAction(bool isChecked);
  148. public delegate void CheckToggleAction<T>(T tag, bool isChecked);
  149. private static MenuItem DoAddCheckItem(ItemsControl menu, string caption, CheckToggleAction click, bool isChecked, bool enabled, int index = -1)
  150. {
  151. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  152. item.Click += (o, e) =>
  153. {
  154. click(item.IsChecked);
  155. };
  156. ItemsControlInsert(menu, item, index);
  157. return item;
  158. }
  159. private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
  160. {
  161. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  162. item.Tag = tag;
  163. item.Click += (o, e) =>
  164. {
  165. click((T)(o as MenuItem)!.Tag, item.IsChecked);
  166. };
  167. ItemsControlInsert(menu, item, index);
  168. return item;
  169. }
  170. private static Separator DoAddSeparator(ItemsControl menu, int index)
  171. {
  172. var separator = new Separator();
  173. ItemsControlInsert(menu, separator, index);
  174. return separator;
  175. }
  176. private static Separator? DoAddSeparatorIfNeeded(ItemsControl menu, int index)
  177. {
  178. if (menu.Items.Count == 0) return null;
  179. var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
  180. if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
  181. var last = menu.Items[lastIndex];
  182. if (last is Separator) return null;
  183. var separator = new Separator();
  184. ItemsControlInsert(menu, separator, index);
  185. return separator;
  186. }
  187. private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
  188. {
  189. while(menu.Items.Count > 0 && menu.Items[0] is Separator)
  190. {
  191. menu.Items.RemoveAt(0);
  192. }
  193. while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
  194. {
  195. menu.Items.RemoveAt(menu.Items.Count - 1);
  196. }
  197. }
  198. public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
  199. public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
  200. public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  201. public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  202. public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
  203. public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
  204. public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  205. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  206. public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  207. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  208. public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  209. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  210. public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  211. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  212. public static MenuItem AddCheckItem(this ContextMenu menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
  213. => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
  214. public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
  215. => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
  216. #endregion
  217. }
  218. }