views:

39

answers:

2

Given the following XMAL why is no vertical scrollbar for the ListBox which is bound to an ObservableCollection of 100 strings. If I change the height of the second row from * to something fixed like 500 then a scrollbar appears, but obviously i want the row height to be what ever is available (which is what I understand * to mean)

<UserControl x:Class="SimpleStack.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <DataTemplate x:Key="ListBoxItemTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Place holder"/><TextBlock Text="{Binding}"/>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="Azure">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="The Text" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
        <ListBox ItemsSource="{Binding ListOfNumbers}" Grid.Row="1" Grid.Column="0"
                 ItemTemplate="{StaticResource ListBoxItemTemplate}"/>
        <TextBlock Text="Place Holder" Grid.Row="1" Grid.Column="1"/>
    </Grid>
</UserControl>
+1  A: 

The * row height is in fact "everything else available" (if you have multiple *s, it would divy that up). I am guessing that your actual issue is that "whatever is available" is infinite. Most likely the usercontrol is being given an unlimited amount of space, so it is expanding to take up as much room as it needs. Make sure you're limiting your usercontrol to the actual visible space and your listbox should get its scrollbar.

Philip Rieck
It does appear that my UserControl is being given an unlimited amount of space, as you suggest. Howerver I have no idea why that would be? The Plug-In is gets 100%x100% (SOP for the VS test page)
Ralph Shillington
Is there a tool to investigate the layout at runtime, similar to the IE developer tools?
Ralph Shillington
Is your `UserControl` the RootVisual of the app or is it contained in another page? If it's contained in another page, check to make sure its containing panel is not a `StackPanel` which would cause it to have infinite available size.
Stephan
@Ralph What Stephan said. Also, regarding your tool request, take a look at Snoop http://snoopwpf.codeplex.com/ - it can help you with layout, bindings, etc. It's something no WPF developer should be without.
Philip Rieck
A: 

My understanding is that because of the measure/arrange layout system you are essentially telling the ListBox it can have all the vertical space it needs without being constrained. The internal ScrollViewer in the default ListBox template is therefore never constrained to trigger the scroll bar to appear.

I can see two ways to fix this for your situation:

-Specify ScrollViewer.VerticalScrollBarVisibility="Visible" on the ListBox to force the internal ScrollViewer to always show the scroll bar.

-Use an actual ScrollViewer to contain the ListBox and let that provide the scrolling capability instead of the one in the internal ListBox (you might have to tweak padding and borders to get it to look right):

<ScrollViewer Grid.Row="1" Grid.Column="0">
    <ListBox ItemsSource="{Binding ListOfNumbers}"
             ItemTemplate="{StaticResource ListBoxItemTemplate}"/>
</ScrollViewer>

I would prefer the second way because it would only show the vertical scroll bar if it was really necessary.

Dan Auclair