Reputation: 79
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>
Upvotes: 0
Views: 627
Reputation: 1
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Upvotes: 0
Reputation: 79
I fixed it, simply adding the Height property in the ListBox control
Upvotes: 1
Reputation: 137148
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
.
Upvotes: 0
Reputation: 27095
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
Upvotes: 0