简体   繁体   中英

ScrollViewer in a listbox

I´m filling a listbox with a specific data, the code works fine, but I need to add a scrollviewer to the listbox because there are many elements inside, I tried to use a ScrolViewer and put inside the listbox, but doesn't work, here is the code

<StackPanel x:Name="Sites" Grid.Row="1"  Orientation="Vertical">
    <ListBox x:Name="ListSites" >
       <ListBox.ItemTemplate>
          <DataTemplate>
            <Button Width="460" Height="120" Click="click" Name="btn">
             <Button.Content>
              <StackPanel Orientation="Vertical" Height="100" Width="460">
                <TextBlock Width="460" Name="txtname" FontSize="22" Text="{Binding name}" Height="40" Foreground="CadetBlue" />
                <TextBlock Width="460" Name="txtUrl" FontSize="22" Text="{Binding Url}" Height="60"/>
               </StackPanel>
              </Button.Content>
              </Button>
             </DataTemplate>
         </ListBox.ItemTemplate>
      </ListBox>
  </StackPanel>

我修复了它,只需在ListBox控件中添加Height属性

If the ListBox is not given infinite space for height then it should automatically scroll when items overflow its boundaries. For instance, if the second Grid row outside your stackpanel has explicit or 'star' height set, the scrollbar in the listbox will show up automatically.

See also: Silverlight: Difficulty with ScrollViewer

You shouldn't need to add a ScrollViewer to your ListBox . It will start scrolling when it runs out of room.

However, because you've put the ListBox inside a StackPanel it won't ever think it's run out of room because the StackPanel grows infinitely in the direction of it's orientation to accommodate its contents.

You'll need to use a different container for your ListBox .

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM