Skip to content

Instantly share code, notes, and snippets.

@mrlacey
Created December 8, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlacey/d4f3d6b1c6439a3abe703d67798901d7 to your computer and use it in GitHub Desktop.
Save mrlacey/d4f3d6b1c6439a3abe703d67798901d7 to your computer and use it in GitHub Desktop.
UWP Grid ShowGridLines
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);
}
}
@mrlacey
Copy link
Author

mrlacey commented Dec 8, 2018

Use it like this

<Grid local:GridLines.AreVisible="True">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

  ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment