wpf 如何以编程方式设置网格行和列位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3745594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Grid row and column positions programmatically
提问by Vinod
I have two Grids inside a Stackpanel. The first grid is named as GridX. Initially, inside the grid, there is a 2D array of Textboxes(RowDefs/ColumnDefs). The TextBox definition in XAML is
我在 Stackpanel 中有两个网格。第一个网格被命名为 GridX。最初,在网格内部,有一个二维文本框数组(RowDefs/ColumnDefs)。XAML 中的 TextBox 定义是
<TextBox x:Name="A1" Grid.Row="4" Grid.Column="5" TextAlignment="Center" />
I want to add a TextBlockprogrammatically in the same position as part of GridX.
我想以编程方式在与 GridX 的一部分相同的位置添加一个TextBlock。
The effect must be like this
效果一定是这样的
<TextBlock Grid.Row="4" Grid.Column="5"
HorizontalAlignment="Left" VerticalAlignment="Top" Text="10" FontSize="8"/>
How to add this. I have tried this:
这个怎么加。我试过这个:
TextBlock tblock = new TextBlock();
GridX.SetColumn(tblock, cIndex);
GridX.SetRow(tblock, rIndex);
But failed.
但是失败了。
Again I tried this:
我再次尝试了这个:
int rIndex = Grid.GetRow(txtBox);
int cIndex = Grid.GetColumn(txtBox);
TextBlock tblock = new TextBlock();
tblock.VerticalAlignment = VerticalAlignment.Top;
tblock.HorizontalAlignment = HorizontalAlignment.Left;
tblock.FontSize = 8;
tblock.Text = rc[i, j - 1];
Grid.SetColumn(tblock, cIndex);
Grid.SetRow(tblock, rIndex);
txtBox.MaxLength = 1;
Now the problem is that TextBlock is not visible. TextBox hides it. I appreciate your help.
现在的问题是 TextBlock 不可见。文本框隐藏它。我感谢您的帮助。
回答by John Bowen
For attached properties you can either call SetValue on the object for which you want to assign the value:
对于附加属性,您可以在要为其分配值的对象上调用 SetValue:
tblock.SetValue(Grid.RowProperty, 4);
Or call the static Set method (not as an instance method like you tried) for the property on the owner type, in this case SetRow:
或者为所有者类型上的属性调用静态 Set 方法(不是像您尝试的那样作为实例方法),在本例中为 SetRow:
Grid.SetRow(tblock, 4);
回答by Dave
Here is an example which might help someone:
这是一个可能对某人有所帮助的示例:
Grid test = new Grid();
test.ColumnDefinitions.Add(new ColumnDefinition());
test.ColumnDefinitions.Add(new ColumnDefinition());
test.RowDefinitions.Add(new RowDefinition());
test.RowDefinitions.Add(new RowDefinition());
test.RowDefinitions.Add(new RowDefinition());
Label t1 = new Label();
t1.Content = "Test1";
Label t2 = new Label();
t2.Content = "Test2";
Label t3 = new Label();
t3.Content = "Test3";
Label t4 = new Label();
t4.Content = "Test4";
Label t5 = new Label();
t5.Content = "Test5";
Label t6 = new Label();
t6.Content = "Test6";
Grid.SetColumn(t1, 0);
Grid.SetRow(t1, 0);
test.Children.Add(t1);
Grid.SetColumn(t2, 1);
Grid.SetRow(t2, 0);
test.Children.Add(t2);
Grid.SetColumn(t3, 0);
Grid.SetRow(t3, 1);
test.Children.Add(t3);
Grid.SetColumn(t4, 1);
Grid.SetRow(t4, 1);
test.Children.Add(t4);
Grid.SetColumn(t5, 0);
Grid.SetRow(t5, 2);
test.Children.Add(t5);
Grid.SetColumn(t6, 1);
Grid.SetRow(t6, 2);
test.Children.Add(t6);
回答by KobCoder
for (int i = 0; i < 6; i++)
{
test.ColumnDefinitions.Add(new ColumnDefinition());
Label t1 = new Label();
t1.Content = "Test" + i;
Grid.SetColumn(t1, i);
Grid.SetRow(t1, 0);
test.Children.Add(t1);
}
回答by Kenlly Acosta
Try this:
尝试这个:
Grid grid = new Grid(); //Define the grid
for (int i = 0; i < 36; i++) //Add 36 rows
{
ColumnDefinition columna = new ColumnDefinition()
{
Name = "Col_" + i,
Width = new GridLength(32.5),
};
grid.ColumnDefinitions.Add(columna);
}
for (int i = 0; i < 36; i++) //Add 36 columns
{
RowDefinition row = new RowDefinition();
row.Height = new GridLength(40, GridUnitType.Pixel);
grid.RowDefinitions.Add(row);
}
for (int i = 0; i < 36; i++)
{
for (int j = 0; j < 36; j++)
{
Label t1 = new Label()
{
FontSize = 10,
FontFamily = new FontFamily("consolas"),
FontWeight = FontWeights.SemiBold,
BorderBrush = Brushes.LightGray,
BorderThickness = new Thickness(2),
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
};
Grid.SetRow(t1, i);
Grid.SetColumn(t1, j);
grid.Children.Add(t1); //Add the Label Control to the Grid created
}
}