Reputation: 8271
I have a WPF Grid I want to insert some images into at runtime. The MSDN at
http://msdn.microsoft.com/en-us/library/system.windows.controls.grid%28v=vs.110%29.aspx
gives this example, using text not images but I assume the concept is the same . . .
// Create the application's main window
mainWindow = new Window();
mainWindow.Title = "Grid Sample";
// Create the Grid
Grid myGrid = new Grid();
myGrid.Width = 250;
myGrid.Height = 100;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = true;
// Define the Columns
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
// Define the Rows
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
RowDefinition rowDef4 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
myGrid.RowDefinitions.Add(rowDef4);
// Add the first text cell to the Grid
TextBlock txt1 = new TextBlock();
txt1.Text = "2005 Products Shipped";
txt1.FontSize = 20;
txt1.FontWeight = FontWeights.Bold;
Grid.SetColumnSpan(txt1, 3);
Grid.SetRow(txt1, 0);
The grid gets drawn fine but no text appears. And I don't understand it anyway because the Text part of the example doesn't reference "myGrid" - how does it know what grid is being referred-to? I tried replacing Grid" with "myGrid" but then the compiler just says "cannot be accessed with an instance reference; qualify it with a type name instead".
So how do I insert an item into an arbitrary Grid coordinate at runtime?
Upvotes: 3
Views: 2738
Reputation: 61339
You missed a crucial line from that example:
myGrid.Children.Add(txt1);
That actually puts the text block into the layout (by adding it to the grid).
The reason you cant use myGrid
in the function calls is that Grid.Row
and Grid.Column
are Attached Properties you don't set them the way you would think (or set them up in XAML the way you would think). See MSDN for more information on how this works (linked to the SetColumn function).
The "Grid" control looks at that attached property for its children and places the item accordingly, thus you don't need a reference to myGrid1
when setting the Column/Row.
MSDN for Attached Properties: http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx
Upvotes: 1
Reputation: 9270
The only thing that that example is missing is
Grid.Children.Add(txt1);
at the very end. Basically, you use Grid.SetRow
, Grid.SetColumn
, Grid.SetRowSpan
and Grid.SetColumnSpan
on your control to set where it will be in the grid and how many cells it will take up, and then all you have to do is Add
it to the grid's Children
for it to actually show up.
So if you want to add an arbitrary control into your Grid
at runtime, just use these functions and you should be set.
Upvotes: 1