Dulan wasala
Dulan wasala

Reputation: 1

Xamarin Forms Grid edit child element at certain Location

So I created a Grid and filled it in using a for loop. I wish to change the color of a specific label (ex. Change the color of the label at [5,6] to red.) I was wondering what would be the syntax to do so?

How I Filled the Grid (C#)

        public void printgrid(Grid Game_Grid, int max_x, int max_y)
        {
            for (int i = 0; i < max_x; i++)
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(Game_Grid.WidthRequest / max_x, GridUnitType.Star);
                Game_Grid.ColumnDefinitions.Add(c);
            }
            for (int j = 0; j < max_y; j++)
            {
                RowDefinition r = new RowDefinition();
                r.Height = new GridLength(Game_Grid.HeightRequest / max_y, GridUnitType.Star);
                Game_Grid.RowDefinitions.Add(r);
            }
            for (int y = 0; y < max_y; y++)
            {
                for (int x = 0; x < max_x; x++)
                {
                    Label cell = new Label
                    {
                        HorizontalOptions = LayoutOptions.Fill
                        ,
                        VerticalOptions = LayoutOptions.Fill,
                        WidthRequest = Game_Grid.WidthRequest / max_x,
                        HeightRequest = Game_Grid.HeightRequest / max_y,
                    };
                    if (y % 2 == 1)
                    {
                        if (x % 2 == 0)
                        {
                            cell.Background = new SolidColorBrush(Color.FromRgb(0, 102, 204));
                        }
                        else
                        {
                            cell.Background = new SolidColorBrush(Color.FromRgb(51, 153, 255));
                        }
                    }
                    else
                    {
                        if (x % 2 == 0)
                        {
                            cell.Background = new SolidColorBrush(Color.FromRgb(51, 153, 255));
                        }
                        else
                        {
                            cell.Background = new SolidColorBrush(Color.FromRgb(0, 102, 204));
                        }
                    }


                    Game_Grid.Children.Add(cell, x, y);
                }
            }
        }

xaml Code

            <Grid x:Name="Game_Grid"  BackgroundColor="RoyalBlue" HeightRequest="350" WidthRequest="350" RowSpacing="1" ColumnSpacing="1">


            </Grid>

Upvotes: 0

Views: 212

Answers (1)

Sugitha
Sugitha

Reputation: 163

We can get the children from the grid with specific rows and columns using the below query where Parentgrid is the name of the grid. "specifiedlabel" is the label in the particular grid of row 1 and column 1.

 List<Label> specifiedlabel= Parentgrid.Children.OfType<Label>().Where(c => Grid.GetRow(c) == 1 && Grid.GetColumn(c) == 1).ToList();
 specifiedlabel[0].TextColor = Color.Pink;

Hope it helps!!

Upvotes: 2

Related Questions