0

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

3
  • 1
    How do you want the Grid instances to be displayed? What do you want the screen to look like? More than likely, the right way to do this is to put your Grid property values into some data container, then bind a collection of those data container objects to an ItemsControl.ItemsSource property, and configure the ItemsControl's ItemTemplate and ItemsPanel. But without more specific information in your question, it's impossible to say exactly how you would do that. Commented Feb 18, 2015 at 8:18
  • What are you trying to do ? Display a set of tiles in a window ? Looks like you're doing it wrong... Commented Feb 18, 2015 at 9:09
  • It's simple, I want to create a multiple grid controls in an array and add them to my window. Commented Feb 19, 2015 at 7:58

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't really answer the question.
0

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.

Comments

0

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]);

3 Comments

Close, but Is there something like a Stack Panel that doesn't stack stuff? It lets you put it anywhere?
well there are lots of layout options you can find them here and here
or as Watta mentioned u can use canvas
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.