using System.Collections.Generic;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Controls.Primitives;
|
using System.Windows.Media;
|
|
namespace HMI.UI.DpUtil
|
{
|
public class UniformGridHelper
|
{
|
public static double GetInnerBorder(DependencyObject obj)
|
{
|
return (double)obj.GetValue(InnerBorderProperty);
|
}
|
|
public static void SetInnerBorder(DependencyObject obj, object value)
|
{
|
double dv = double.Parse(value.ToString());
|
obj.SetValue(InnerBorderProperty, dv);
|
}
|
|
public static readonly DependencyProperty InnerBorderProperty =
|
DependencyProperty.RegisterAttached("InnerBorder", typeof(object), typeof(UniformGrid),
|
new PropertyMetadata(0, OnInnerBorderChanged));
|
|
public static Brush GetInnerBorderBrush(DependencyObject obj)
|
{
|
return (Brush)obj.GetValue(InnerBorderBrushProperty);
|
}
|
public static void SetInnerBorderBrush(DependencyObject obj, Brush value)
|
{
|
obj.SetValue(InnerBorderBrushProperty, value);
|
}
|
public static readonly DependencyProperty InnerBorderBrushProperty =
|
DependencyProperty.RegisterAttached("InnerBorderBrush", typeof(Brush), typeof(UniformGrid),
|
new PropertyMetadata(null, OnInnerBorderChanged));
|
|
public static string GetInnerBorderIgnore(DependencyObject obj)
|
{
|
return (string)obj.GetValue(InnerBorderIgnoreProperty);
|
}
|
public static void SetInnerBorderIgnore(DependencyObject obj, string value)
|
{
|
obj.SetValue(InnerBorderIgnoreProperty, value);
|
}
|
|
public static readonly DependencyProperty InnerBorderIgnoreProperty =
|
DependencyProperty.RegisterAttached("InnerBorderIgnore", typeof(string),
|
typeof(UniformGrid), new PropertyMetadata("None", OnInnerBorderChanged));
|
|
private static void OnInnerBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
if (d is UniformGrid grid)
|
{
|
if (e.Property == InnerBorderProperty || e.Property == InnerBorderIgnoreProperty)
|
{
|
grid.Loaded += GridInnerBorder_Loaded;
|
return;
|
}
|
}
|
}
|
|
private static void GridInnerBorder_Loaded(object sender, RoutedEventArgs e)
|
{
|
UniformGrid grid = sender as UniformGrid;
|
RefreshGridInnerBorder(grid);
|
grid.Loaded -= GridInnerBorder_Loaded;
|
}
|
|
public static void RefreshGridInnerBorder(UniformGrid grid)
|
{
|
double width = GetInnerBorder(grid);
|
if (width <= 0) return;
|
var rowCount = grid.Rows;
|
var columnCount = grid.Columns;
|
int totalCount = rowCount * columnCount;
|
|
string baseData = GetInnerBorderIgnore(grid);
|
HashSet<string> data = new HashSet<string>(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");
|
|
rowCount = (grid.Children.Count / columnCount)
|
+ (grid.Children.Count % columnCount > 0 ? 1 : 0);
|
var brush = GetInnerBorderBrush(grid) ?? Brushes.Black;
|
|
for (int i = 0; i < grid.Children.Count; i++)
|
{
|
var child = grid.Children[i];
|
|
if (child is Control control)
|
{
|
|
int col = i % columnCount;
|
int row = i / columnCount;
|
|
double left = (col == 0 && ignoreLeft) ? 0 : width;
|
double top = (row == 0 && ignoreTop) ? 0 : width;
|
|
double right = ignoreRight ? 0 : ((col == columnCount - 1) ? width : 0);
|
double bottom = ignoreBottom ? 0 : ((row == rowCount - 1) ? width : 0);
|
|
var bt = new Thickness(left, top, right, bottom);
|
|
control.BorderBrush = brush;
|
control.BorderThickness = bt;
|
}
|
|
}
|
}
|
}
|
}
|