本文介绍了如何动态创建类似矩阵的网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式添加行和列定义并使用元素填充这些单元格,但是我得到的结果看起来就像一个单元格,我甚至看不到文本块。知道我在这里做错了什么吗?

I'm trying to programmatically add row and column definitions and fill those cells with elements, however what I get as a result looks just like one cell, I don't even see text blocks. Any idea what am I doing wrong here?

public MainPage()

{
InitializeComponent( );

var rows = 5;
var cols = 4;

for(int i = 0; i< rows; i ++)
{
var rowDef = new RowDefinition {Height = new GridLength(0,GridUnitType.Star)};
grid.RowDefinitions.Add(rowDef);
}

for(int i = 0; i< cols; i ++)
{
var colDef = new ColumnDefinition {Width = new GridLength(0,GridUnitType .Star)};
grid.ColumnDefinitions.Add(colDef);
}

for(int row = 0; row< rows; row ++)
for(int col = 0; col< cols; col ++)
{
var control = new TextBlock {Text =" foo" };

Grid.SetRow(control,row);
Grid.SetColumn(control,col);

grid.Children.Add(control);
}
}

{ InitializeComponent(); var rows = 5; var cols = 4; for (int i = 0; i < rows; i++) { var rowDef = new RowDefinition { Height = new GridLength(0, GridUnitType.Star) }; grid.RowDefinitions.Add(rowDef); } for (int i = 0; i < cols; i++) { var colDef = new ColumnDefinition { Width = new GridLength(0, GridUnitType.Star) }; grid.ColumnDefinitions.Add(colDef); } for (int row = 0; row < rows; row++) for (int col = 0; col < cols; col++) { var control = new TextBlock { Text = "foo" } ; Grid.SetRow(control, row); Grid.SetColumn(control, col); grid.Children.Add(control); } }

P.S。谷歌搜索这个问题会带来很多来自这个论坛的结果,但是当我点击它时,我只是进入主页,好的工作微软搞砸了!当然,对相同关键词的内部搜索不会返回任何结果......

P.S. Googling this question brings up many results from this forum, but when I click on it I just get to the main page, good job Microsoft for screwing it up! And of course the internal search for the same key words doesn't return any results...

推荐答案

//var rowDef = new RowDefinition { Height = new GridLength(0, GridUnitType.Star) };
var rowDef = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };


这篇关于如何动态创建类似矩阵的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:58