Created
December 8, 2018 16:19
-
-
Save mrlacey/d4f3d6b1c6439a3abe703d67798901d7 to your computer and use it in GitHub Desktop.
UWP Grid ShowGridLines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GridLines : DependencyObject | |
{ | |
public static readonly DependencyProperty AreVisibleProperty = | |
DependencyProperty.RegisterAttached( | |
"AreVisible", | |
typeof(Boolean), | |
typeof(GridLines), | |
new PropertyMetadata(false, OnPropertyChanged) | |
); | |
private static Grid owner; | |
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
if (d is Grid grid && e.NewValue is bool showLines) | |
{ | |
SetLineVisibility(grid, showLines); | |
} | |
} | |
// Change these 3 values as per your preferences | |
private static Color LineColor = Colors.Black; | |
private const int LineThickness = 8; | |
private const bool ShowOuterBorder = false; | |
private const string GridLineTag = "GRIDLINE"; | |
protected static void SetLineVisibility(Grid grid, bool showLines) | |
{ | |
if (showLines) | |
{ | |
var cols = grid.ColumnDefinitions.Count; | |
var rows = grid.RowDefinitions.Count; | |
for (int col = 0; col < cols; col++) | |
{ | |
for (int row = 0; row < rows; row++) | |
{ | |
var thickness = ShowOuterBorder | |
? new Thickness(col == 0 ? LineThickness : 0, row == 0 ? LineThickness : 0, LineThickness, LineThickness) | |
: new Thickness(0, 0, col < grid.ColumnDefinitions.Count - 1 ? LineThickness : 0, row < grid.RowDefinitions.Count - 1 ? LineThickness : 0); | |
var border = new Border(); | |
border.BorderBrush = new SolidColorBrush(LineColor); | |
border.BorderThickness = thickness; | |
border.Tag = GridLineTag; | |
Grid.SetColumn(border, col); | |
Grid.SetRow(border, row); | |
grid.Children.Add(border); | |
} | |
} | |
} | |
else | |
{ | |
foreach (var gridChild in grid.Children.Reverse()) | |
{ | |
if (gridChild is Border border) | |
{ | |
if (border.Tag.ToString() == GridLineTag) | |
{ | |
grid.Children.Remove(border); | |
} | |
} | |
} | |
} | |
} | |
public static void SetAreVisible(Grid grid, Boolean value) | |
{ | |
grid.SetValue(AreVisibleProperty, value); | |
grid.Loaded += (sender, args) => { SetLineVisibility(grid, value); }; | |
} | |
public static Boolean GetAreVisible(Grid grid) | |
{ | |
return (Boolean)grid.GetValue(AreVisibleProperty); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it like this