这是我的代码:
Grid gameboard = new Grid();
gameboard.HorizontalAlignment = HorizontalAlignment.Left;
gameboard.VerticalAlignment = VerticalAlignment.Top;
gameboard.Width = Window.Current.Bounds.Width;
gameboard.Height = Window.Current.Bounds.Width;
Border border = new Border();
border.BorderThickness = new Thickness(1);
border.BorderBrush = new SolidColorBrush(Colors.Blue);
for (int j=0;j<7;j++)
{
gameboard.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int i = 0; i < 7; i++)
{
gameboard.RowDefinitions.Add(new RowDefinition());
}
我是学习者,现在我想展示我的网格线,有人能帮帮我吗? 非常感谢!
答案 0 :(得分:4)
由于您正在学习,我将帮助您开始努力,让您和处于类似情况的其他人进入下一步。
从以下代码开始,进行调整,学习,研究,最重要的是有乐趣。
<强> XAML 强>
<Grid Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="30" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</Grid>
<强> CODE 强>
public MainPage()
{
this.InitializeComponent();
DataContext = this;
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Grid gameboard = new Grid();
gameboard.HorizontalAlignment = HorizontalAlignment.Stretch;
gameboard.VerticalAlignment = VerticalAlignment.Stretch;
for (int j = 0; j < 7; j++)
{
var cd = new ColumnDefinition();
cd.Width = new GridLength(1, GridUnitType.Star);
var rd = new RowDefinition();
rd.Height = new GridLength(1, GridUnitType.Star);
gameboard.ColumnDefinitions.Add(cd);
gameboard.RowDefinitions.Add(rd);
}
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 7; i++)
{
Border border = new Border();
border.BorderThickness = new Thickness(1);
border.BorderBrush = new SolidColorBrush(Colors.Blue);
border.HorizontalAlignment = HorizontalAlignment.Stretch;
border.VerticalAlignment = VerticalAlignment.Stretch;
var tb = new TextBlock();
tb.Text = string.Format($"i={i}; j={j}");
tb.Margin = new Thickness(4);
Grid.SetColumn(border, j);
Grid.SetRow(border, i);
border.Child = tb;
gameboard.Children.Add(border);
}
}
LayoutRoot.Children.Add(gameboard);
}
<强> RESULT 强>
<强>概要强>
这是一个开始。它不是完美,并且要使内边框不比边缘厚,需要花费很少的精力,但不应该太难。提示:考虑如何使用border.BorderThickness = new Thickness(l, t, r, b);
,其中l / t / r / b为1或0,具体取决于i / j。我甚至可以把这个问题作为面试问题;可能是一个有趣的讨论。
答案 1 :(得分:0)
您可以使用Grid.ShowGridLines属性并添加网格线。
gameboard.ShowGridLines = true;