tags:

views:

28

answers:

2

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

A: 

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);
John Bowen
+1  A: 

You need to actually add the TextBlock to the Grid

GridX.Children.Add(tblock);
Nir