tags:

views:

426

answers:

2

I have a listbox that displays a list of WPF controls. My problem is that the vertical scrollbar is show but is disabled even when there are enough items that the listbox should be scrollable. One other possibly relevant fact is that this is contained in an Integration.ElementHost.

WPF noobie, Jim

Here is the XAML for the listbox:

  // for brevity I removed the Margin and Tooltip attributes

  <Grid x:Class="Xyzzy.NoteListDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <StackPanel Name="stackPanel" Orientation="Vertical"
                ScrollViewer.VerticalScrollBarVisibility="Visible">
        <StackPanel Orientation="Horizontal">
            <CheckBox Name="AllRecent" IsChecked="False" >View All Recent</CheckBox>
            <CheckBox Name="AscendingOrder" IsChecked="False">Descending Order</CheckBox>
            <Button Name="btnTextCopy" Click="btnCopyText_Click">Copy All</Button>
        </StackPanel>
        <ListBox Name="NoteList"
                 ScrollViewer.CanContentScroll="True"
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
        </ListBox>
      </StackPanel>
  </Grid>

And the XAML for the control displayed in each listbox item:

  <UserControl x:Class="Xyzzy.NoteDisplay"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Grid>
      <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
          <TextBlock Name="Heading" FontSize="10">Note Heading</TextBlock>
          <Button Name="btnCopyText" Height="20" FontSize="12"
                          Click="btnCopyText_Click">Copy
          </Button>
        </StackPanel>
        <TextBlock Name="Body" FontSize="14">Note Body</TextBlock>
      </StackPanel>
    </Grid>
  </UserControl>
+1  A: 

Heya, I suspect what might be happening is that your ListBox is expanding enough for every item however the ListBox is actually disappearing off the bottom of the Containing Control.

Does the ListBox actually stop properly or does it just seem to disappear? Try setting a MaxHeight on the ListBox and see if that makes the scrollbar appear. You should be able to set the VerticalScrollBarVisibility to Auto to have it only appear when needed.

I should have mentioned that I tried restricting the size of the Listbox exactly as you suggest. The result was no improvement.
Jim Reineri
+3  A: 

I have had problems with scroll bar visibility when using a StackPanel. I think it is because the StackPanel is always as big as it needs to be to contain all of it's children. Try reorganizing the layout to remove the StackPanel (use a Grid instead) and see if that helps.

John Myczek
When I put Listbox into Grid rather than StackPanel the problem disapeared.I this a WPF code bug, WPF design bug or deliberate WPF behavior??
Jim Reineri
I think it is just the way the StackPanel works. In your case the StackPanel was as tall as the ListBox needed to be without scrolling, then the StackPanel got "cut off" at the bottom.
John Myczek