using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; namespace Plugin.BurnIn { public class GridProperties { public static bool GetShowBorder(DependencyObject obj) { return (bool)obj.GetValue(ShowBorderProperty); } public static void SetShowBorder(DependencyObject obj, bool value) { obj.SetValue(ShowBorderProperty, value); } public static readonly DependencyProperty ShowBorderProperty = DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(Grid), new PropertyMetadata(false, OnShowBorderChanged)); public static int GetBorderSpace(DependencyObject obj) { return (int)obj.GetValue(BorderSpaceProperty); } public static void SetBorderSpace(DependencyObject obj, int value) { obj.SetValue(BorderSpaceProperty, value); } public static readonly DependencyProperty BorderSpaceProperty = DependencyProperty.RegisterAttached("BorderSpace", typeof(int), typeof(Grid), new PropertyMetadata(0, OnShowBorderChanged)); private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Grid grid) { if (e.Property == ShowBorderProperty) { grid.Loaded += GridInnerBorderLoaded; return; } else if (e.Property == BorderSpaceProperty) { grid.Loaded += GridSpaceOnLoaded; return; } } else if (d is UniformGrid uniformGrid) { uniformGrid.Loaded += UniformGridOnLoad; return; } } private static void GridInnerBorderLoaded(object sender, RoutedEventArgs e) { if (!(sender is Grid grid)) return; var rowCount = grid.RowDefinitions.Count; var columnCount = grid.ColumnDefinitions.Count; int totalCount = rowCount * columnCount; int width = 1; string baseData = "None"; HashSet data = new HashSet(baseData.Split(',')); bool ignoreTop = data.Contains("All") || data.Contains("Top"), ignoreBottom = data.Contains("All") || data.Contains("Bottom"), ignoreLeft = data.Contains("All") || data.Contains("Left"), ignoreRight = data.Contains("All") || data.Contains("Right"); foreach (var child in grid.Children) { int row = Grid.GetRow((UIElement)child); int col = Grid.GetColumn((UIElement)child); var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0, row == 0 ? (ignoreTop ? 0 : width) : 0, (col == columnCount - 1 && ignoreRight) ? 0 : width, width); if (child is UserControl control) { control.BorderBrush = Brushes.Black; control.BorderThickness = bt; } if (child is Label label) { label.BorderBrush = Brushes.Black; label.BorderThickness = bt; } if (child is TextBox box) { box.BorderBrush = Brushes.Black; box.BorderThickness = bt; } } /// 弃用 //for (int i = 0; i < totalCount; i++) //{ // int row = i / columnCount; // int col = i % columnCount; // if (i > grid.Children.Count - 1) { // continue; // } // var cell = grid.Children[i]; // var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0, // row == 0 ? (ignoreTop ? 0 : width) : 0, // (col == columnCount - 1 && ignoreRight) ? 0 : width, // width); // if (cell is UserControl child) // { // child.BorderBrush = Brushes.Black; // child.BorderThickness = bt; // } // if (cell is Label label) // { // label.BorderBrush = Brushes.Black; // label.BorderThickness = bt; // } // if (cell is TextBox box) // { // box.BorderBrush = Brushes.Black; // box.BorderThickness = bt; // } //} grid.Loaded -= GridInnerBorderLoaded; } private static void GridSpaceOnLoaded(object sender, RoutedEventArgs e) { if (!(sender is Grid grid)) return; int space = GetBorderSpace(grid); if (space <= 0) { return; } var rowCount = grid.RowDefinitions.Count; var columnCount = grid.ColumnDefinitions.Count; int totalCount = rowCount * columnCount; for (int i = 0; i < totalCount; i++) { int row = i / columnCount; int col = i % columnCount; if (i > grid.Children.Count - 1) { continue; } var cell = grid.Children[i]; var bt = new Thickness(col == 0 ? space : 0, row == 0 ? space : 0, space, space); if (cell is UserControl child) { child.Margin = bt; } } grid.Loaded -= GridSpaceOnLoaded; } private static void UniformGridOnLoad(object sender, RoutedEventArgs e) { if (!(sender is UniformGrid grid)) return; //var rowCount = grid.Rows; int columnCount = grid.Columns; int totalCount = grid.Children.Count; int rowCount = totalCount / columnCount; int width = 1; for (int i = 0; i < grid.Children.Count; i++) { int row = i / columnCount; int col = i % columnCount; var child = grid.Children[i]; string baseData = "None"; HashSet data = new HashSet(baseData.Split(',')); bool ignoreTop = data.Contains("All") || data.Contains("Top"), ignoreBottom = data.Contains("All") || data.Contains("Bottom"), ignoreLeft = data.Contains("All") || data.Contains("Left"), ignoreRight = data.Contains("All") || data.Contains("Right"); var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0, row == 0 ? (ignoreTop ? 0 : width) : 0, (col == columnCount - 1 && ignoreRight) ? 0 : width, (row == rowCount && ignoreBottom) ? 0 : width); if (child is UserControl control) { control.BorderBrush = Brushes.Black; control.BorderThickness = bt; } if (child is Label label) { label.BorderBrush = Brushes.Black; label.BorderThickness = bt; } if (child is TextBox box) { box.BorderBrush = Brushes.Black; box.BorderThickness = bt; } } grid.Loaded -= UniformGridOnLoad; } } }