locked
Metro App: How i can define row and column defination dynamically RRS feed

  • Question

  • Hi All,

    please help me to define row and column defination dynamically in Grid. I want  some thing like this

    where the column can increase to 5.8 and 5.9 or it may decrease based on the input.

    Regard's

    Gopikris

    Wednesday, August 21, 2013 2:23 PM

Answers

  • You can create RowDefinitions and ColumnDefinitions in code similar to how you would do so in Xaml. Here's a quick sample snippet.

                RowDefinition rowDef = new RowDefinition();
                MyGrid.RowDefinitions.Add(rowDef);
                for (int i = 51; i < 60; i++)
                {
                    ColumnDefinition colDef = new ColumnDefinition();
    
                    MyGrid.ColumnDefinitions.Add(colDef);
                    colDef.Width = new GridLength(50, GridUnitType.Pixel);
                    TextBlock text = new TextBlock();
                    text.Text = (i / 10.0).ToString();
                    MyGrid.Children.Add(text);
                    text.SetValue(Grid.RowProperty, 0);
                    text.SetValue(Grid.ColumnProperty, i - 51);
                    
                }

    This isn't something I'd use in a production app, but I'm not sure what you are really looking for here. You'd probably be better off with a databound ItemsControl of some sort, but without context I don't know what exactly you would want to do here.

    --Rob

    Thursday, August 22, 2013 12:55 AM
    Moderator