如何以编程方式设置网格行和列位置 [英] How to set Grid row and column positions programmatically

查看:90
本文介绍了如何以编程方式设置网格行和列位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Stackpanel内的两个网格。
第一个网格被命名为GridX。
最初在网格内有一个二维数组文本框(RowDefs / ColumnDefs)。
XAML中的TextBox定义为:

 < TextBox x:Name =A1Grid.Row =4 Grid.Column =5TextAlignment =Center/> 

我想要在 TextBlock GridX。



效果必须像这样

 < TextBlock Grid .Row =4Grid.Column =5
Horizo​​ntalAlignment =LeftVerticalAlignment =TopText =10FontSize =8/>

如何添加此项。
我试过这个:

  TextBlock tblock = new TextBlock(); 
GridX.SetColumn(tblock,cIndex);
GridX.SetRow(tblock,rIndex);

但是失败了。

这:

  int rIndex = Grid.GetRow(txtBox); 
int cIndex = Grid.GetColumn(txtBox);

TextBlock tblock = new TextBlock();
tblock.VerticalAlignment = VerticalAlignment.Top;
tblock.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left;
tblock.FontSize = 8;
tblock.Text = rc [i,j - 1];

Grid.SetColumn(tblock,cIndex);
Grid.SetRow(tblock,rIndex);


txtBox.MaxLength = 1;

现在问题在于TextBlock不可见.TextBox隐藏它。
您可以帮助解决方案对于附加的属性,您可以在要为其分配对象的对象上调用SetValue值:

  tblock.SetValue(Grid.RowProperty,4); 

或者调用静态Set方法(不像您尝试的实例方法)所有者类型,在这种情况下SetRow:

  Grid.SetRow(tblock,4); 


I have two Grids inside a Stackpanel. 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

<TextBox x:Name="A1" Grid.Row="4" Grid.Column="5" TextAlignment="Center" />

I want to add a TextBlock programamtically in the same position as part of GridX.

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. Can you help

解决方案

For attached properties you can either call SetValue on the object for which you want to assign the value:

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:

Grid.SetRow(tblock, 4);

这篇关于如何以编程方式设置网格行和列位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆