tags:

views:

2373

answers:

2

I'm building a project that loads data from a webservice into a TreeView Control. When the TreeView is contained on the LayoutRoot grid by itself and it's height is set to Auto, if the contents extend beyond the vertical or horizontal limits of the treeview scrollbars appear automatically, as expected.

If that same TreeView control is placed inside a StackPanel, it's behavior changes. When data extends past it's limits, no scrollbar appears and data simply clips off the edge with no access to it. If I manually set the height of the TreeView in this scenario, the scrollbars will appear again as expected.

Clearly there seems to be some interaction between the StackPanel and TreeView that I'm not seeing.

Can anyone explain this and suggest an appropriate way to handle the situation?

Per request in comments:

The following XAML works fine and renders scrollbars as expected (notice the Height on the TreeView is Specified):

<StackPanel >
      <controls:TreeView ItemTemplate="{StaticResource MainEntryIndexTemplate}" 
             x:Name="CodeBookIndexTreeView" Height="500" />
</StackPanel>

The following also displays scrollbars as expected (notice no StackPanel, but the TreeView's Height is set to Auto):

  <controls:TreeView ItemTemplate="{StaticResource MainEntryIndexTemplate}" 
         x:Name="CodeBookIndexTreeView" Height="Auto" />

Finally, this code fails in that the TreeView will not display scrollbars and data scrolls off the bottom and/or right hand side of the control (notice the TreeView is in a StackPanel and the TreeView's Height is set to Auto):

<StackPanel >
          <controls:TreeView ItemTemplate="{StaticResource MainEntryIndexTemplate}" 
                 x:Name="CodeBookIndexTreeView" Height="Auto" />
    </StackPanel>

Cheers,

Steve

A: 

Yeh - StackPanels don't seem to be a great control for containing variable sized objects.

Similarly; I have had trouble with DataTemplates composed of textblocks within a stackpanel, used as ListItem templates within a ListBox. Intention was for the text block to wrap, but instead it just continues beyond the bounds of the list box, for which there is no horizontal scroll. Best I've managed so far was to fix the width of the Grid containing the stackpanels but not yet found a satisfactory solution.. would love to know if there is one!

Nick,I just read a post that says a vertically aligned StackPanel will allow infinite height to child objects, which woulkd in this case stop the scroll bars from appearing. I'm going to check this out and get back.
Steve Brouillard
+2  A: 

Managed to get this one figured out with some help from the Silverlight.net forums. You can read the original question and answer here. It turns out that a StackPanel oriented vertically (the defaul orientation) will give infinite size to its children, so since the TreeView has infinite size, the scroll bars will never display. This does NOT happen when using a grid as your layout element.

The following XAML will render the TreeView scroll bars as expected. (Notice that the TreeView is contained in row 2 of a Grid, not a StackPanel and that the Height of the TreeView is set to Auto.) There are some additional controls in this code snippet because that was the reason for the extra container in the first place:

<Grid>
      <Grid.RowDefinitions>
       <RowDefinition Height="Auto"/>
       <RowDefinition Height="Auto"/>
       <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <StackPanel Orientation="Horizontal" Grid.Row="0">
       <TextBlock Text="Enter Search Term" />
       <TextBox x:Name="SearchTermTextBox" Width="200" KeyUp="SearchTermTextBox_KeyUp"/>
      </StackPanel>
      <Button x:Name="SearchButton" Content="Search" Click="SearchButton_Click" Width="100" 
          HorizontalAlignment="Left" Grid.Row="1" />
      <controls:TreeView ItemTemplate="{StaticResource MainEntryIndexTemplate}" 
             x:Name="CodeBookIndexTreeView" Height="Auto" Grid.Row="2" />
</Grid>
Steve Brouillard