WPFUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
  99. {
  100. if (depObj != null)
  101. //ContentControl cc = depObj as ContentControl;
  102. //if (cc != null)
  103. //{
  104. // if (cc.Content == null)
  105. // yield return null;
  106. // if (cc.Content is T)
  107. // yield return cc.Content as T;
  108. // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
  109. // yield return child;
  110. //}
  111. //else
  112. for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  113. {
  114. var child = VisualTreeHelper.GetChild(depObj, i);
  115. if (child is null)
  116. continue;
  117. if (child is T t)
  118. yield return t;
  119. foreach (var childOfChild in FindVisualChildren<T>(child))
  120. yield return childOfChild;
  121. }
  122. }
  123. #region Menu Utils
  124. private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
  125. {
  126. if (index != -1)
  127. {
  128. menu.Items.Insert(index, item);
  129. }
  130. else
  131. {
  132. menu.Items.Add(item);
  133. }
  134. }
  135. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
  136. {
  137. var item = new MenuItem { Header = caption, IsEnabled = enabled };
  138. if (image != null)
  139. item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
  140. ItemsControlInsert(menu, item, index);
  141. return item;
  142. }
  143. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
  144. {
  145. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  146. if (click != null)
  147. {
  148. item.Click += (o, e) =>
  149. {
  150. click();
  151. };
  152. }
  153. return item;
  154. }
  155. private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
  156. {
  157. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  158. item.Tag = tag;
  159. item.Click += (o, e) =>
  160. {
  161. click((T)(o as MenuItem)!.Tag);
  162. };
  163. return item;
  164. }
  165. public delegate void CheckToggleAction(bool isChecked);
  166. public delegate void CheckToggleAction<T>(T tag, bool isChecked);
  167. private static MenuItem DoAddCheckItem(ItemsControl menu, string caption, CheckToggleAction click, bool isChecked, bool enabled, int index = -1)
  168. {
  169. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  170. item.Click += (o, e) =>
  171. {
  172. click(item.IsChecked);
  173. };
  174. ItemsControlInsert(menu, item, index);
  175. return item;
  176. }
  177. private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
  178. {
  179. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  180. item.Tag = tag;
  181. item.Click += (o, e) =>
  182. {
  183. click((T)(o as MenuItem)!.Tag, item.IsChecked);
  184. };
  185. ItemsControlInsert(menu, item, index);
  186. return item;
  187. }
  188. private static Separator DoAddSeparator(ItemsControl menu, int index)
  189. {
  190. var separator = new Separator();
  191. ItemsControlInsert(menu, separator, index);
  192. return separator;
  193. }
  194. private static Separator? DoAddSeparatorIfNeeded(ItemsControl menu, int index)
  195. {
  196. if (menu.Items.Count == 0) return null;
  197. var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
  198. if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
  199. var last = menu.Items[lastIndex];
  200. if (last is Separator) return null;
  201. var separator = new Separator();
  202. ItemsControlInsert(menu, separator, index);
  203. return separator;
  204. }
  205. private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
  206. {
  207. while(menu.Items.Count > 0 && menu.Items[0] is Separator)
  208. {
  209. menu.Items.RemoveAt(0);
  210. }
  211. while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
  212. {
  213. menu.Items.RemoveAt(menu.Items.Count - 1);
  214. }
  215. }
  216. public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
  217. public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
  218. public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  219. public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  220. public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
  221. public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
  222. public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  223. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  224. public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  225. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  226. public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  227. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  228. public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
  229. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  230. public static MenuItem AddCheckItem(this ContextMenu menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
  231. => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
  232. public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
  233. => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
  234. #endregion
  235. }
  236. }