Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar in Listbox not working

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">
    <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">
    <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>
like image 507
Jim Reineri Avatar asked Aug 12 '09 17:08

Jim Reineri


2 Answers

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 its children. Try reorganizing the layout to remove the StackPanel (use a Grid instead) and see if that helps.

like image 100
John Myczek Avatar answered Nov 07 '22 04:11

John Myczek


You just need to introduce Height property, like this:

<ListBox Height="200" 
         Name="NoteList"
         ScrollViewer.CanContentScroll="True"
         ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
like image 11
Mert Akcakaya Avatar answered Nov 07 '22 03:11

Mert Akcakaya