123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using Image = System.Windows.Controls.Image;
- namespace InABox.WPF
- {
- public static class WPFUtils
- {
- public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
- where T : DependencyObject
- {
- DependencyObject? parent = dependencyObject;
- do
- {
- parent = LogicalTreeHelper.GetParent(parent);
- } while(parent != null && parent is not T);
- return parent as T;
- }
- public static int GetRow(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetRow(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
- public static int GetRowSpan(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetRowSpan(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
- public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan, int colspan)
- {
- element.SetValue(Grid.ColumnProperty, column);
- element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
- element.SetValue(Grid.RowProperty, row);
- element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
- }
- public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
- {
- if (depObj != null)
- //ContentControl cc = depObj as ContentControl;
- //if (cc != null)
- //{
- // if (cc.Content == null)
- // yield return null;
- // if (cc.Content is T)
- // yield return cc.Content as T;
- // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
- // yield return child;
- //}
- //else
- for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
- {
- var child = VisualTreeHelper.GetChild(depObj, i);
- if (child is null)
- continue;
- if (child is T t)
- yield return t;
- foreach (var childOfChild in FindVisualChildren<T>(child))
- yield return childOfChild;
- }
- }
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled)
- {
- var item = new MenuItem { Header = caption, IsEnabled = enabled };
- if (image != null)
- item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
- menu.Items.Add(item);
- return item;
- }
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled)
- {
- var item = DoAddMenuItem(menu, caption, image, enabled);
- if (click != null)
- {
- item.Click += (o, e) =>
- {
- click();
- };
- }
- return item;
- }
- private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled)
- {
- var item = DoAddMenuItem(menu, caption, image, enabled);
- item.Tag = tag;
- item.Click += (o, e) =>
- {
- click((T)(o as MenuItem)!.Tag);
- };
- return item;
- }
- public delegate void CheckToggleAction<T>(T tag, bool isChecked);
- private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled)
- {
- var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
- item.Tag = tag;
- item.Checked += (o, e) =>
- {
- click((T)(o as MenuItem)!.Tag, true);
- };
- item.Unchecked += (o, e) =>
- {
- click((T)(o as MenuItem)!.Tag, false);
- };
- menu.Items.Add(item);
- return item;
- }
- public static Separator AddSeparator(this ContextMenu menu)
- {
- var separator = new Separator();
- menu.Items.Add(separator);
- return separator;
- }
- public static Separator? AddSeparatorIfNeeded(this ContextMenu menu)
- {
- if (menu.Items.Count == 0) return null;
- var last = menu.Items[menu.Items.Count - 1];
- if (last is Separator) return null;
- var separator = new Separator();
- menu.Items.Add(separator);
- return separator;
- }
- public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true)
- => DoAddMenuItem(menu, caption, image, click, enabled);
- public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true)
- => DoAddMenuItem(menu, caption, image, click, enabled);
- public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
- => DoAddMenuItem(menu, caption, image, tag, click, enabled);
- public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
- => DoAddMenuItem(menu, caption, image, tag, click, enabled);
- public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true)
- => DoAddCheckItem<T>(menu, caption, tag, click, isChecked, enabled);
- }
- }
|