locked
Setting Dynamic Height and widht of a Grid in Silverlight RRS feed

  • Question

  • Hi,

    How can I set dynamic Height and Widht of a Grid Control in Silverlight in xaml page.

    Thanks

    Rahul

    Tuesday, November 15, 2011 6:05 AM

All replies

  • Hello, 

    You'll have to define your rows and colmuns like in my example. Then you'll have to type "auto" in width/height to get dynamic widths or heights :-)

    I hope i helped you ;-)

    <Grid>
    <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
                <ColumnDefinition Height="auto"/>
                <ColumnDefinition Height="*"/>
    </Grid.ColumnDefinitions>
            <sdk:Label Content="TestTestTest" Grid.Row="0" Grid.Column="0"/>
    </Grid>
    Tuesday, November 15, 2011 6:53 AM
  • No, still the problem remains.

    If I increase the pixels settings of my computer, my grid becomes short and it shows the white borders.

    Thanks

    Rahul

    Tuesday, November 15, 2011 7:39 AM
  • Is your Grid in other Control like StackPanel? if yes , you should set the the control( here StackPanel) VertialAlignment and HorizontalAlignment to Stretch

    Wednesday, November 16, 2011 3:03 AM
  • <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
                <ColumnDefinition Height="auto"/>
                <ColumnDefinition Height="*"/>
    </Grid.ColumnDefinitions>
            <sdk:Label Content="TestTestTest" Grid.Row="0" Grid.Column="0"/>
    </Grid>
    This will make your grid take all the available space :-)
    Wednesday, November 16, 2011 6:57 AM
  • Hi,

    I am using the following code but still my Grid is not getting streached, can anyone tell me whats wrong with my code.

    <Grid x:Name="grdTest" Background="#e2e7f2" ShowGridLines="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20"></RowDefinition>
                            <RowDefinition Height="5"></RowDefinition>
                            <RowDefinition Height="25"></RowDefinition>
                            <RowDefinition Height="25"></RowDefinition>
                            <RowDefinition Height="25"></RowDefinition>
                            <RowDefinition Height="25"></RowDefinition>
                            <RowDefinition Height="60*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
    <sdk:Label Content="Test" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="4,2,0,0" Name="lbTest" VerticalAlignment="Top" Width="120" FontWeight="Bold" Grid.RowSpan="2" />
    </Grid>
    Monday, December 12, 2011 7:25 AM
  • Its hard to tell how you are determining that it isn't "stretched", but since ALL your columns are Auto, the width of the grid I would expect to only be as wide as the data you put in the columns.

    You could try making your last column be * instead of Auto.  Does that do what you want?

    Monday, December 12, 2011 1:46 PM
  • Hi rahul. Mtiede and others are right auto will work as soon as any content need to be larger but * should cover the whole space. According to my understanding, you want to spread the grid over whole page or you want to see height and width dynamically as long as your control need space , they will cover the space according to their requirement. I would simply suggest that create a clean silver light application then add a silverlight page/window and In that xaml, keep a single row definition in grid and set its height Auto and place content in that row . I mean using grid attached property Grid.Row=0 and check that. Keep it simple then you will be able to trace your mistake.

    Monday, December 12, 2011 2:28 PM