Reputation:
I want to add an array of grids to my WPF window:
Grid[] Tiles = new Grid[20];
public void LoadTile()
{
for (int X = 0; X < Tiles.Length; X++)
{
Tiles[X] = new Grid();
Tiles[X].Height = (TileData[X].SizeY * 90) - 10;
Tiles[X].Width = (TileData[X].SizeY * 90) - 10;
Tiles[X].Margin = new Thickness(0 + (TileData[X].PositionX * 90), 216 + (TileData[X].PositionY * 90), 0, 0);
Tiles[X].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Tiles[X].VerticalAlignment = System.Windows.VerticalAlignment.Center;
Tiles[X].Visibility = System.Windows.Visibility.Visible;
SolidColorBrush Brush1 = new SolidColorBrush(Colors.Black);
Brush1.Opacity = 0.2;
Tiles[X].Background = Brush1;
}
}
That's what I have.
(BTW: I do have a method calling that one I just didn't include it here)
I added:
Nine_Window.Content = Tiles[X];
But it made it so all I could display was one of them, because each time the loop did that piece of code again it overwrote the last one
Upvotes: 0
Views: 2236
Reputation:
Okay, Muds nearly got my answer but I'm gonna use a Canvas instead of a Stack Panel. If you didn't get what I meant, it's simple, I wanted to create a multiple grid controls in an array and add them to my window.
Upvotes: 0
Reputation: 4116
Well I donot second your approach but if you want to continue with it, do not add your Grids like this
Nine_Window.Content = Tiles[X];
instead add a stackPanel to NineWindow.Content
<Window ....>
<Grid>
<StackPanel x:Name="myStackPanel"></StackPanel>
</Grid>
and then in code behind
myStackPanel.Children.Add(Tile[X]);
Upvotes: 0
Reputation: 11763
Your usual use for a grid (let's assume 3x3) will look something along the following in the XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
Regarding your problem with setting the Content, You are setting it to a specific tile instead of to the array. But Again, it'll be easy to do from the XAML I believe, and simply initialize it from code if you need to.
Upvotes: 1
Reputation: 61
I think what you are actually looking for is row and column definitions of a grid. Add as many as you need by executing:
Grid Tile = new Grid()
// create new columns
ColumDefintion columnDefinition = new ColumnDefinition()
columnDefinition.Height = ... // set height here
Tile.ColumnDefinitions.Add(columnDefinition);
// create a row
Tile.RowDefinitions.Add(new RowDefinition());
Otherwise your changes will affect the whole grid object.
Upvotes: 0