I want to dynamically create a 1-row grid and add some TextBlocks to it, assigning/associating each one to a different column in the grid. I’ve got this code:
SolidColorBrush samHagar = new SolidColorBrush(Colors.Red);
System.Windows.Thickness mrg = new Thickness(2);
// Create a Grid
Grid grd = new Grid();
. . . // TODO: add columns
//...add the Grid to the StackPanel
spNufan.Children.Add(grd);
// Create TextBlock and dynamically add it to the Grid
TextBlock tbDynamo = new TextBlock();
tbDynamo.Background = samHagar;
tbDynamo.TextWrapping = TextWrapping.Wrap;
//tbDynamo.Grid.Column = 0; <- no go, Joe!
tbDynamo.Margin = mrg;
tbDynamo.TextAlignment = TextAlignment.Left;
tbDynamo.VerticalAlignment = VerticalAlignment.Center;
tbDynamo.Text = "Whatever";
spNufan.Children.Add(grd);
How can I affiliate my TextBlock (“tbDynamo”) with my Grid (“grd”)?
Set the object’s
Grid.Columnproperty, then add the object to the GridAs a side note, you don’t actually need to set it to 0 since items in a
Gridwill default toGrid.Row=0andGrid.Column=0unless otherwise specified.