Containing Silverlight Lists and DataGrids in the Browser Window

In a typical Silverlight line-of-business application we have Lists, Grids, DataGrids, and StackPanels.  We populate a list and it flows down and off the browser page.  When we have a ScrollViewer, it will scroll the whole page including edit controls and graphics and not just the list that's tall and flowing off the page.  The good news is that we can easily contain the list in the viewable area with the few simple steps listed below.

  1. Remove the ScrollViewer from the page if it exists.
  2. Contain all upper area graphics and edit controls in a RowDefinitions of explicit size or Auto.
  3. Contain the lower List(s) in a final Grid RowDefinition with the Star notation to fill the remainder of the window using the outer-most Grid as a parent container.
  4. Optionally specify for the List properties,  VerticalAlignment="Top" and VerticalContentAlignment="Stretch".  Otherwise the List and its borders will stretch to the bottom of the screen even if it has no items.  Either way may be preferred. 

The following screen shots demonstrate this with a simple GridSplitter to size either side.  Notice the List on the left is using two rows compared to the list on the right.  The list on the right isn’t flowing to the bottom and uses only one row.  Both lists flow to the bottom of the browser window and no further, regardless of the number of items.

XAML rocks







    <Grid x:Name="LayoutRoot"
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        <sdk:Label Content="List 1"
                   FontWeight="Bold" />
        <sdk:Label Grid.Column="2"
                   Content="List 2"
                   FontWeight="Bold" />
        <ListBox Grid.Row="1"
                 Margin="10" />
        <sdk:GridSplitter Grid.Row="1"
                          Grid.RowSpan="2" />
        <ListBox Grid.Row="2"
                 VerticalContentAlignment="Stretch" />
        <sdk:Label Grid.Row="1"
                   Content="(Edit Controls)"
                   HorizontalAlignment="Center" />


private void MainPage_Loaded(object sender, RoutedEventArgs e)

    for (int i = 1; i <= 100; i++)
        string listItem = "List Item " + i.ToString();