wpfNewBie
wpfNewBie

Reputation: 37

Issue in creating a 10 by 10 matrix in WPF

Hi I am trying to create a 10 by 10 matrix in WPF by using the below code

private void MainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            MainGrid.RowDefinitions.Add(new RowDefinition());
            MainGrid.ColumnDefinitions.Add(new ColumnDefinition());

            //Create rectangle
            Rectangle rect = new Rectangle();
            rect.Width = 10;
            rect.Height = 10;

            for (int i = 0; i < 10; i++) // for rows
            {
                for (int j = 0; j < 10; j++) //for columns
                {                    
                    if (i % 2 == 0 || j % 2 == 0)
                    {
                        rect.Stroke = Brushes.Green;
                        rect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255,255,0,0));
                    }
                    else
                    {
                        rect.Stroke = Brushes.DarkOrchid;
                        rect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 204, 10, 20));
                    }
                    Grid.SetColumn(rect, j);                    
                }
                Grid.SetRow(rect, i);
            }
            MainGrid.Children.Add(rect); 
        }

The xaml is defined as under

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid x:Name="MainGrid" Loaded="MainGrid_Loaded">

    </Grid>
</Window>

But it is showing only 1 rectangle.

What wrong I am doing .. please help

Thanks

Upvotes: 2

Views: 333

Answers (3)

Elad Katz
Elad Katz

Reputation: 7601

Note that in those situations, when you need same-sized cells in a grid, it's way easier to use UniformGrid instead of Grid. It's also better performance-wise.

Upvotes: 0

kyrylomyr
kyrylomyr

Reputation: 12632

You need to create new rectangle in loop, not only once.

private void MainGrid_Loaded(object sender, RoutedEventArgs e)
{   
    //Add rows and columns.
    for (int i = 0; i < 10; i++)
    {
        //Add row.
        MainGrid.RowDefinitions.Add(new RowDefinition());
        //Add column.
        MainGrid.ColumnDefinitions.Add(new ColumnDefinition());
    } 

    for (int i = 0; i < 10; i++) // for rows
    {
        for (int j = 0; j < 10; j++) //for columns
        { 
             //Create rectangle
             Rectangle rect = new Rectangle();
             rect.Width = 10;
             rect.Height = 10;   

             if (i % 2 == 0 || j % 2 == 0)
             {
                  rect.Stroke = Brushes.Green;
                  rect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255,255,0,0));
             }
             else
             {
                  rect.Stroke = Brushes.DarkOrchid;
                  rect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 204, 10, 20));
             }
             Grid.SetRow(rect, i);
             Grid.SetColumn(rect, j); 
             MainGrid.Children.Add(rect);                   
        }
    }
}

Upvotes: 1

Gabe
Gabe

Reputation: 86768

Your Grid only has 1 row and 1 column. You must define 10 rows and 10 columns in your Grid.

Also, you only have a single Rectangle that you change the color of 100 times. You have to put the new Rectangle(), SetRow, SetColumn, and Add inside your inner loop for this to work.

Upvotes: 0

Related Questions