Simone
Simone

Reputation: 2430

How to define a grid layout

I still do not manage to understand how define a flexible Grid layout... Id like to have a Grid with n rows (i dont know how many) and m columns (i dont know how many).. I want cells have a specific height and width (50 and 50)....

This is how i can set row and column definition:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="350" />
    <ColumnDefinition Width="130" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="2*" />
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

But in this way im just setting two columns and 3 rows... There is no way to set a Column definition and a row definition for each column and grid?

Thanx

Upvotes: 0

Views: 3801

Answers (2)

max
max

Reputation: 645

Going on the initial question, this is how you can start C# :-

         Grid Layout = new Grid();

         foreach (your condition)
         { 
         //
             Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
             Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50)});
             Layout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
         }

Upvotes: 1

Anobik
Anobik

Reputation: 4899

This is how you do it

public MainPage()
    {
        InitializeComponent();
        var def = new ColumnDefinition();
        def.Width = new GridLength(100);
        gdTest.ColumnDefinitions.Add(def);
        StackPanel sp = new StackPanel();
        sp.Background = new SolidColorBrush( Colors.Brown);
        sp.SetValue(Grid.ColumnProperty, 1);
        gdTest.Children.Add(sp);

    }

Xaml

<Grid x:Name="gdTest" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="5,0,5,0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0" x:Name="TestStack" Background="Red"/>


</Grid>

add the code and test :)

Upvotes: 0

Related Questions