WPFUtils.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. using InABox.Core;
  11. using InABox.Wpf;
  12. using Image = System.Windows.Controls.Image;
  13. using System.Runtime.InteropServices;
  14. using System.Windows.Interop;
  15. namespace InABox.WPF;
  16. public class FuncTemplateSelector : DataTemplateSelector
  17. {
  18. public Func<object, DependencyObject, FrameworkElement?> TemplateFunc { get; set; }
  19. public FuncTemplateSelector(Func<object, DependencyObject, FrameworkElement?> templateFunc)
  20. {
  21. TemplateFunc = templateFunc;
  22. }
  23. public override DataTemplate SelectTemplate(object item, DependencyObject container)
  24. {
  25. return TemplateGenerator.CreateDataTemplate(() =>
  26. {
  27. return TemplateFunc(item, container);
  28. });
  29. }
  30. }
  31. public static class WPFUtils
  32. {
  33. public static void RegisterClasses()
  34. {
  35. CoreUtils.RegisterClasses(typeof(WPFUtils).Assembly);
  36. }
  37. public static void MoveToCenter(this Window window)
  38. {
  39. if (!GetCursorPos(out POINT cursorPoint))
  40. return;
  41. IntPtr mainWindow = MonitorFromPoint(new POINT() { x = 1, y = 1 }, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
  42. IntPtr monitorHandle = MonitorFromPoint(cursorPoint, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
  43. MONITORINFO monitorInfo = new() { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
  44. if (!GetMonitorInfo(monitorHandle, ref monitorInfo))
  45. return;
  46. IntPtr windowHandle = new WindowInteropHelper(window).EnsureHandle();
  47. if (!GetWindowPlacement(windowHandle, out WINDOWPLACEMENT windowPlacement))
  48. return;
  49. int left = monitorInfo.rcWork.left + Math.Max(0, (int)((monitorInfo.rcWork.Width - windowPlacement.rcNormalPosition.Width) / 2D));
  50. int top = monitorInfo.rcWork.top + Math.Max(0, (int)((monitorInfo.rcWork.Height - windowPlacement.rcNormalPosition.Height) / 2D));
  51. windowPlacement.rcNormalPosition = new RECT(left, top, windowPlacement.rcNormalPosition.Width, windowPlacement.rcNormalPosition.Height);
  52. SetWindowPlacement(windowHandle, ref windowPlacement);
  53. }
  54. [DllImport("User32.dll", SetLastError = true)]
  55. [return: MarshalAs(UnmanagedType.Bool)]
  56. private static extern bool GetCursorPos(out POINT lpPoint);
  57. [DllImport("User32.dll")]
  58. private static extern IntPtr MonitorFromPoint(POINT pt, MONITOR_DEFAULTTO dwFlags);
  59. private enum MONITOR_DEFAULTTO : uint
  60. {
  61. MONITOR_DEFAULTTONULL = 0x00000000,
  62. MONITOR_DEFAULTTOPRIMARY = 0x00000001,
  63. MONITOR_DEFAULTTONEAREST = 0x00000002,
  64. }
  65. [DllImport("User32.dll")]
  66. [return: MarshalAs(UnmanagedType.Bool)]
  67. private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
  68. [StructLayout(LayoutKind.Sequential)]
  69. private struct MONITORINFO
  70. {
  71. public uint cbSize;
  72. public RECT rcMonitor;
  73. public RECT rcWork;
  74. public uint dwFlags;
  75. }
  76. [DllImport("User32.dll", SetLastError = true)]
  77. [return: MarshalAs(UnmanagedType.Bool)]
  78. private static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
  79. [DllImport("User32.dll", SetLastError = true)]
  80. [return: MarshalAs(UnmanagedType.Bool)]
  81. private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
  82. [StructLayout(LayoutKind.Sequential)]
  83. private struct WINDOWPLACEMENT
  84. {
  85. public uint length;
  86. public uint flags;
  87. public uint showCmd;
  88. public POINT ptMinPosition;
  89. public POINT ptMaxPosition;
  90. public RECT rcNormalPosition;
  91. }
  92. [StructLayout(LayoutKind.Sequential)]
  93. private struct POINT
  94. {
  95. public int x;
  96. public int y;
  97. }
  98. [StructLayout(LayoutKind.Sequential)]
  99. private struct RECT
  100. {
  101. public int left;
  102. public int top;
  103. public int right;
  104. public int bottom;
  105. public int Width => right - left;
  106. public int Height => bottom - top;
  107. public RECT(int x, int y, int width, int height)
  108. {
  109. left = x;
  110. top = y;
  111. right = x + width;
  112. bottom = y + height;
  113. }
  114. }
  115. #region Setters/Triggers etc
  116. public static Style AddSetter(this Style style, DependencyProperty property, object value)
  117. {
  118. style.Setters.Add(new Setter(property, value));
  119. return style;
  120. }
  121. public static DataTrigger AddSetter(this DataTrigger trigger, DependencyProperty property, object value)
  122. {
  123. trigger.Setters.Add(new Setter(property, value));
  124. return trigger;
  125. }
  126. public static DataTrigger AddDataTrigger(this Style style)
  127. {
  128. var trigger = new DataTrigger();
  129. style.Triggers.Add(trigger);
  130. return trigger;
  131. }
  132. public static DataTrigger AddDataTrigger(this Style style, Binding binding, object value)
  133. {
  134. var trigger = new DataTrigger() { Binding = binding, Value = value };
  135. style.Triggers.Add(trigger);
  136. return trigger;
  137. }
  138. #endregion
  139. public static SolidColorBrush ToBrush(this System.Windows.Media.Color color, double opacity = 1.0)
  140. {
  141. return new SolidColorBrush(color) { Opacity = opacity };
  142. }
  143. #region Multi-Binding
  144. public static MultiBinding CreateMultiBinding(IMultiValueConverter? converter = null, string? format = null, object? parameter = null)
  145. {
  146. return new MultiBinding
  147. {
  148. Converter = converter,
  149. ConverterParameter = parameter,
  150. StringFormat = format
  151. };
  152. }
  153. public static MultiBinding AddBinding(this MultiBinding multi, BindingBase binding)
  154. {
  155. multi.Bindings.Add(binding);
  156. return multi;
  157. }
  158. #endregion
  159. #region Binding
  160. public static Binding CreateBinding<T, TProperty>(
  161. T source,
  162. Expression<Func<T, TProperty>> expression,
  163. IValueConverter? converter = null,
  164. string? format = null)
  165. {
  166. return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
  167. {
  168. Source = source,
  169. Converter = converter,
  170. StringFormat = format
  171. };
  172. }
  173. public static Binding CreateBinding<T, TProperty>(
  174. Expression<Func<T, TProperty>> expression,
  175. IValueConverter? converter = null,
  176. BindingMode mode = BindingMode.Default,
  177. string? format = null,
  178. RelativeSource? relativeSource = null)
  179. {
  180. return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
  181. {
  182. Converter = converter,
  183. StringFormat = format,
  184. Mode = mode,
  185. RelativeSource = relativeSource
  186. };
  187. }
  188. public static DataTrigger Bind<T, TProperty, TValue>(
  189. this DataTrigger trigger,
  190. T source,
  191. Expression<Func<T, TProperty>> expression,
  192. TValue value,
  193. IValueConverter<TProperty, TValue>? converter,
  194. string? format = null)
  195. {
  196. trigger.Binding = CreateBinding(source, expression, converter, format);
  197. trigger.Value = value;
  198. return trigger;
  199. }
  200. public static DataTrigger Bind<T, TProperty, TValue>(
  201. this DataTrigger trigger,
  202. Expression<Func<T, TProperty>> expression,
  203. TValue value,
  204. IValueConverter<TProperty, TValue>? converter,
  205. BindingMode mode = BindingMode.Default,
  206. string? format = null,
  207. RelativeSource? relativeSource = null)
  208. {
  209. trigger.Binding = CreateBinding(expression, converter, mode, format, relativeSource: relativeSource);
  210. trigger.Value = value;
  211. return trigger;
  212. }
  213. public static DataTrigger Bind<T, TProperty>(
  214. this DataTrigger trigger,
  215. T source,
  216. Expression<Func<T, TProperty>> expression,
  217. TProperty value,
  218. IValueConverter? converter = null,
  219. string? format = null)
  220. {
  221. trigger.Binding = CreateBinding(source, expression, converter, format);
  222. trigger.Value = value;
  223. return trigger;
  224. }
  225. public static DataTrigger Bind<T, TProperty>(
  226. this DataTrigger trigger,
  227. Expression<Func<T, TProperty>> expression,
  228. TProperty value,
  229. IValueConverter? converter = null,
  230. BindingMode mode = BindingMode.Default,
  231. string? format = null,
  232. RelativeSource? relativeSource = null)
  233. {
  234. trigger.Binding = CreateBinding(expression, converter, mode, format, relativeSource: relativeSource);
  235. trigger.Value = value;
  236. return trigger;
  237. }
  238. public static void Bind<T, TProperty>(
  239. this FrameworkElement element,
  240. DependencyProperty property,
  241. T source,
  242. Expression<Func<T, TProperty>> expression,
  243. IValueConverter? converter = null,
  244. string? format = null)
  245. {
  246. element.SetBinding(
  247. property,
  248. CreateBinding(source, expression, converter, format)
  249. );
  250. }
  251. public static void Bind<T, TProperty>(
  252. this FrameworkElement element,
  253. DependencyProperty property,
  254. Expression<Func<T, TProperty>> expression,
  255. IValueConverter? converter = null,
  256. BindingMode mode = BindingMode.Default,
  257. string? format = null,
  258. RelativeSource? relativeSource = null)
  259. {
  260. element.SetBinding(
  261. property,
  262. CreateBinding(expression, converter, mode, format, relativeSource: relativeSource)
  263. );
  264. }
  265. public static FrameworkElementFactory Bind<T, TProperty>(
  266. this FrameworkElementFactory element,
  267. DependencyProperty property,
  268. T source,
  269. Expression<Func<T, TProperty>> expression,
  270. IValueConverter? converter = null,
  271. string? format = null)
  272. {
  273. element.SetBinding(
  274. property,
  275. CreateBinding(source, expression, converter, format)
  276. );
  277. return element;
  278. }
  279. public static FrameworkElementFactory Bind<T, TProperty>(
  280. this FrameworkElementFactory element,
  281. DependencyProperty property,
  282. Expression<Func<T, TProperty>> expression,
  283. IValueConverter? converter = null,
  284. BindingMode mode = BindingMode.Default,
  285. string? format = null,
  286. RelativeSource? relativeSource = null)
  287. {
  288. element.SetBinding(
  289. property,
  290. CreateBinding(expression, converter, mode, format, relativeSource: relativeSource ?? RelativeSource.TemplatedParent)
  291. );
  292. return element;
  293. }
  294. #endregion
  295. #region Converters
  296. public static IMultiValueConverter WrapConverter(this IValueConverter converter, Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null)
  297. {
  298. return new MultiFuncConverter(convert, convertBack, converter);
  299. }
  300. #endregion
  301. public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
  302. where T : DependencyObject
  303. {
  304. DependencyObject? parent = dependencyObject;
  305. do
  306. {
  307. parent = LogicalTreeHelper.GetParent(parent);
  308. } while(parent != null && parent is not T);
  309. return parent as T;
  310. }
  311. public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj, bool recursive = true)
  312. {
  313. if (depObj != null)
  314. for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  315. {
  316. var child = VisualTreeHelper.GetChild(depObj, i);
  317. if (child is null)
  318. continue;
  319. if (child is T t)
  320. yield return t;
  321. if (recursive)
  322. {
  323. foreach (var childOfChild in FindVisualChildren<T>(child))
  324. yield return childOfChild;
  325. }
  326. }
  327. }
  328. public static T? FindChild<T>(this DependencyObject parent, string childName)
  329. where T : FrameworkElement
  330. {
  331. return parent.FindVisualChildren<T>().FirstOrDefault(x => x.Name == childName);
  332. }
  333. #region Grid Children
  334. public static int GetRow(this Grid grid, DependencyObject dependencyObject)
  335. {
  336. while (true)
  337. {
  338. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  339. if (parent == null)
  340. return -1;
  341. if (parent == grid)
  342. return Grid.GetRow(dependencyObject as UIElement);
  343. dependencyObject = parent;
  344. }
  345. }
  346. public static int GetRowSpan(this Grid grid, DependencyObject dependencyObject)
  347. {
  348. while (true)
  349. {
  350. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  351. if (parent == null)
  352. return -1;
  353. if (parent == grid)
  354. return Grid.GetRowSpan(dependencyObject as UIElement);
  355. dependencyObject = parent;
  356. }
  357. }
  358. public static int GetColumn(this Grid grid, DependencyObject dependencyObject)
  359. {
  360. while (true)
  361. {
  362. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  363. if (parent == null)
  364. return -1;
  365. if (parent == grid)
  366. return Grid.GetColumn(dependencyObject as UIElement);
  367. dependencyObject = parent;
  368. }
  369. }
  370. public static int GetColumnSpan(this Grid grid, DependencyObject dependencyObject)
  371. {
  372. while (true)
  373. {
  374. var parent = LogicalTreeHelper.GetParent(dependencyObject);
  375. if (parent == null)
  376. return -1;
  377. if (parent == grid)
  378. return Grid.GetColumnSpan(dependencyObject as UIElement);
  379. dependencyObject = parent;
  380. }
  381. }
  382. public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan = 1, int colspan = 1)
  383. {
  384. element.SetValue(Grid.ColumnProperty, column);
  385. element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
  386. element.SetValue(Grid.RowProperty, row);
  387. element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
  388. }
  389. public static Grid AddChild(this Grid grid, FrameworkElement element, int row, int column, int rowSpan = 1, int colSpan = 1)
  390. {
  391. element.SetGridPosition(row, column, rowSpan, colSpan);
  392. grid.Children.Add(element);
  393. return grid;
  394. }
  395. #endregion
  396. #region Grid Columns + Rows
  397. public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)
  398. {
  399. var colDef = new ColumnDefinition { Width = new GridLength(value, type) };
  400. grid.ColumnDefinitions.Add(colDef);
  401. return colDef;
  402. }
  403. public static ColumnDefinition AddColumn(this Grid grid, double value)
  404. {
  405. var colDef = new ColumnDefinition { Width = new GridLength(value) };
  406. grid.ColumnDefinitions.Add(colDef);
  407. return colDef;
  408. }
  409. public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)
  410. {
  411. var rowDef = new RowDefinition { Height = new GridLength(value, type) };
  412. grid.RowDefinitions.Add(rowDef);
  413. return rowDef;
  414. }
  415. public static RowDefinition AddRow(this Grid grid, double value)
  416. {
  417. var rowDef = new RowDefinition { Height = new GridLength(value) };
  418. grid.RowDefinitions.Add(rowDef);
  419. return rowDef;
  420. }
  421. #endregion
  422. #region Menu Utils
  423. private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
  424. {
  425. if (index != -1)
  426. {
  427. menu.Items.Insert(index, item);
  428. }
  429. else
  430. {
  431. menu.Items.Add(item);
  432. }
  433. }
  434. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
  435. {
  436. var item = new MenuItem { Header = caption, IsEnabled = enabled };
  437. if (image != null)
  438. item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
  439. ItemsControlInsert(menu, item, index);
  440. return item;
  441. }
  442. private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
  443. {
  444. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  445. if (click != null)
  446. {
  447. item.Click += (o, e) =>
  448. {
  449. click();
  450. };
  451. }
  452. return item;
  453. }
  454. private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T>? click, bool enabled, int index = -1)
  455. {
  456. var item = DoAddMenuItem(menu, caption, image, enabled, index);
  457. item.Tag = tag;
  458. if(click != null)
  459. {
  460. item.Click += (o, e) =>
  461. {
  462. click((T)(o as MenuItem)!.Tag);
  463. };
  464. }
  465. return item;
  466. }
  467. public delegate void CheckToggleAction(bool isChecked);
  468. public delegate void CheckToggleAction<T>(T tag, bool isChecked);
  469. private static MenuItem DoAddCheckItem(ItemsControl menu, string caption, CheckToggleAction click, bool isChecked, bool enabled, int index = -1)
  470. {
  471. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  472. item.Click += (o, e) =>
  473. {
  474. click(item.IsChecked);
  475. };
  476. ItemsControlInsert(menu, item, index);
  477. return item;
  478. }
  479. private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
  480. {
  481. var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
  482. item.Tag = tag;
  483. item.Click += (o, e) =>
  484. {
  485. click((T)(o as MenuItem)!.Tag, item.IsChecked);
  486. };
  487. ItemsControlInsert(menu, item, index);
  488. return item;
  489. }
  490. private static Separator DoAddSeparator(ItemsControl menu, int index)
  491. {
  492. var separator = new Separator();
  493. ItemsControlInsert(menu, separator, index);
  494. return separator;
  495. }
  496. private static Separator? DoAddSeparatorIfNeeded(ItemsControl menu, int index)
  497. {
  498. if (menu.Items.Count == 0) return null;
  499. var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
  500. if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
  501. var last = menu.Items[lastIndex];
  502. if (last is Separator) return null;
  503. var separator = new Separator();
  504. ItemsControlInsert(menu, separator, index);
  505. return separator;
  506. }
  507. private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
  508. {
  509. while(menu.Items.Count > 0 && menu.Items[0] is Separator)
  510. {
  511. menu.Items.RemoveAt(0);
  512. }
  513. while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
  514. {
  515. menu.Items.RemoveAt(menu.Items.Count - 1);
  516. }
  517. }
  518. public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
  519. public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
  520. public static Separator AddMenuSeparator(this ItemsControl menu, int index = -1) => DoAddSeparator(menu, index);
  521. public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  522. public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  523. public static Separator? AddMenuSeparatorIfNeeded(this ItemsControl menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
  524. public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
  525. public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
  526. public static void RemoveUnnecessaryMenuSeparators(this ItemsControl menu) => DoRemoveUnnecessarySeparators(menu);
  527. public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  528. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  529. public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  530. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  531. public static MenuItem AddMenuItem(this ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
  532. => DoAddMenuItem(menu, caption, image, click, enabled, index);
  533. public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T>? click, bool enabled = true, int index = -1)
  534. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  535. public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T>? click, bool enabled = true, int index = -1)
  536. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  537. public static MenuItem AddMenuItem<T>(this ItemsControl menu, string caption, Bitmap? image, T tag, Action<T>? click, bool enabled = true, int index = -1)
  538. => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
  539. public static MenuItem AddCheckItem(this ContextMenu menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
  540. => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
  541. public static MenuItem AddCheckMenuItem(this ItemsControl menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
  542. => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
  543. public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
  544. => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
  545. public static MenuItem AddCheckMenuItem<T>(this ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
  546. => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
  547. #endregion
  548. }