locked
Code generated Grid column width auto issue RRS feed

  • Question

  • Hi, I'm currently creating a simple grid in my c# code, which contains in the first row 1 textblock header, in the second one is another grid and than there are some further textblocks in some rows.
    The sub-grid has the same layout and column/row width/height settings than it's parent.

    My problem is now that the the grid is not fit to the bounds. The width of the parent grid is set to 480 and the textblocks in the grids have all textwrapping.wrap.
    The width of the column is set like this: 
                            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

    Does anyone know what I forgot?


    Saturday, April 21, 2012 9:33 PM

Answers

  • Instead of Auto, try Star sizing (in xaml, it would be Width="*"):
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } ); 
    If that doesn't work, post all of your grid code, because the layout can be affected by how the parent controls are set up.
    Here's an article about Grid layout that discusses Auto and Star sizing: http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx

    I suggest that you initially compose your grid using xaml in a test page with sample data. When you see that the layout does what you want, it will be easy to translate to C#.


    Richard Woo
    Sunday, April 22, 2012 3:33 AM

All replies

  • Instead of Auto, try Star sizing (in xaml, it would be Width="*"):
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } ); 
    If that doesn't work, post all of your grid code, because the layout can be affected by how the parent controls are set up.
    Here's an article about Grid layout that discusses Auto and Star sizing: http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx

    I suggest that you initially compose your grid using xaml in a test page with sample data. When you see that the layout does what you want, it will be easy to translate to C#.


    Richard Woo
    Sunday, April 22, 2012 3:33 AM
  • Thanks for your answer.
    I have tried what you have said. I have also found the problem but I'm not sure why it doesn't work.

    This is a simplified code and the colmndefinition with * as width were the problems. Before I had everywhere Auto but it seems that the Auto columndefinitions have no limitation. so it ignores the width property?
    I have now solved it by setting the width at the last columdefinition to * in my c# code

                            <Grid Width="420"
                                <Grid.ColumnDefinitions> 
                                    <ColumnDefinition Width="*"></ColumnDefinition> 
                                </Grid.ColumnDefinitions> 
                                <Grid.RowDefinitions> 
                                    <RowDefinition></RowDefinition> 
                                    <RowDefinition></RowDefinition> 
                                </Grid.RowDefinitions> 
                                <Border Grid.Row="0" Background="White"
                                    <TextBlock Foreground="Black" Text="long long long long long longlong long long long long longHeadline" TextWrapping="Wrap" /> 
                                </Border> 
                                <Grid Grid.Row="1" Grid.Column="0"
                                    <Grid.ColumnDefinitions> 
                                        <ColumnDefinition Width="Auto"></ColumnDefinition> 
                                        <ColumnDefinition Width="*"></ColumnDefinition> 
                                    </Grid.ColumnDefinitions> 
                                    <Grid.RowDefinitions> 
                                        <RowDefinition></RowDefinition> 
                                        <RowDefinition></RowDefinition> 
                                        <RowDefinition></RowDefinition> 
                                    </Grid.RowDefinitions> 
                                    <Border Grid.Row="0" Grid.Column="0"
                                        <TextBlock Text="first Content1" TextWrapping="Wrap" /> 
                                    </Border> 
                                    <Border Grid.Row="0" Grid.Column="1"
                                        <TextBlock Text="long long long long long longlong long long long long long content" TextWrapping="Wrap" /> 
                                    </Border> 
     
                                    <Border Grid.Row="1" Grid.Column="0"
                                        <TextBlock Text="first Content2" TextWrapping="Wrap" /> 
                                    </Border> 
                                    <Border Grid.Row="1" Grid.Column="1"
                                        <TextBlock Text="long long long long long long content" TextWrapping="Wrap" /> 
                                    </Border> 
     
                                    <Border Grid.Row="2" Grid.Column="0"
                                        <TextBlock Text="first Content3" TextWrapping="Wrap" /> 
                                    </Border> 
                                    <Border Grid.Row="2" Grid.Column="1"
                                        <TextBlock Text="long long long long long long content" TextWrapping="Wrap" /> 
                                    </Border> 
     
     
                                </Grid> 
                            </Grid> 
     



    Sunday, April 22, 2012 9:10 AM