ScrollViewer in a listbox - c#

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>

I fixed it, simply adding the Height property in the ListBox control

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>

Related

Is it possible to bind Children in a StackPanel and preserve vertical SnapPoints?

I have a StackPanel inside a ScrollViewer and I would like to bind items and define their datatemplate so I use a ItemsControl. The problem appears to be the ItemsControl does not implement IScrollSnapPointsInfo and this is breaking my vertical snap points associated with the StackPanel children and the ScrollViewer.
These vertical snap points should allow the items bound to the stack panel to align in the vertical center position of the scroll viewer when the user scrolls.
Bellow is some sample code:
<ScrollViewer x:Name="ScrollViewer"
ZoomMode="Disabled"
ZoomSnapPointsType="None"
VerticalSnapPointsType="Mandatory"
VerticalSnapPointsAlignment="Center">
<ScrollViewer.Content>
<Grid>
<!-- Start StackPanel -->
<StackPanel x:Name="StackPanel" Margin="0,500,0,500" Orientation="Vertical">
<StackPanel.Children>
<ItemsControl ItemsSource="{x:Bind Items, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="model:ScrollItem">
<Button Margin="8,0">
<Button.Content>
<Grid>
<TextBlock Text="Hello" Foreground="Black" FontSize="20" />
</Grid>
</Button.Content>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel.Children>
</StackPanel>
<!-- End StackPanel -->
</Grid>
</ScrollViewer.Content>
</ScrollViewer>
Does anyone have experience with this? any advice on how I can restore the vertical snap points and still bind to the stack panel would be great.
Ideally I would like to just bind a list of items to the stack panel and define the template in xaml. This is much better than creating elements in code etc...

WPF listbox template horizontal with line break

In my WPF document i have something like:
<ListBox x:Name="lbNames" Height="400" Width="400">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Element[Icon].Value}" />
<TextBlock Text="{Binding Element[Name].Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But I want this list horizontal and if there is not enough space, I want the next item in a new line.
At the end it should look like a grid.
Optional: If there is not enough space vertically, I want a scrollbar.
You need change items panel to WrapPanel
<ListBox x:Name="lbNames" Height="400" Width="400" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Element[Icon].Value}" />
<TextBlock Text="{Binding Element[Name].Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
What you are describing is a WrapPanel. This element will place your TextBlock's in a Grid format, and if they exceed the width of the WrapPanel, then the next element will be forced onto a new line.
See this tutorial.
To make list horizontal, use
Orientation="Horizontal"
in stackpanel. And for vertical scrollbar if it goes higher than it should, use
ScrollViewer.VerticalScrollBarVisibility="Visible".

A ListBox inside a stackpanel doesn't work

I have these controls:
<StackPanel>
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="ends: 2015"/>
</StackPanel>
ListBox may have any number of items inside it. I want the whole page scroll from top to bottom, but it doesn't scroll down-up. How can I do that? I know the problem is because a scrollable control inside another scrollable one, but don't know how to fix it.
You need to assign the height to the listbox control, as it is taking an auto which means that the height keeps on increasing according to the count of items in it, so its impossible to access the items in the bottom of the control, So either give it a height or instead of stackpanel keep it in a grid with row definitions.
<StackPanel>
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
<ListBox Height="200">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="ends: 2015"/>
</StackPanel>
Try specifying a height for your controls, especially StackPanel and ListBox, even just an "Auto" Height could work for testing. Also, rather than setting an ItemTemplate, which would be more useful if you had a List of items to bind the ListBox to, try some hardcoded values:
<ListBox>
<TextBlock Text="1"/>
<TextBlock Text="Test"/>
<TextBlock Text="Another"/>
<TextBlock Text="testing"/>
<TextBlock Text="Sample"/>
</ListBox>
Try to add more than the ListBox can show, and see if it scrolls.
Try a dock panel instead:
<DockPanel>
<StackPanel DockPanel.Dock="Top">
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
</StackPanel>
<TextBlock DockPanel.Dock="Bottom" Text="ends: 2015"/>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
The ListBox will automatically expand to fill the space. Note: It has to be the last child in the DockPanel. (You can change this functionality using LastChildFill="False")

C# WPF Limit items per row in a Listview

My Listview:
<ListView ItemTemplate="{StaticResource GridViewItemTemplate}" Name="gridView_movies">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Datatemplate of the list:
<DataTemplate x:Key="GridViewItemTemplate">
<StackPanel Orientation="Vertical" >
<Image Width="250" Height="290" Source="{Binding Image}"/>
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="20"/>
</StackPanel>
</DataTemplate>
When i load this, all items are showed in one row, my question is, How can i show only 3 items per row instead of all items in one row.
Thanks for the attention.
Use
<UniformGrid Columns="3" .../>
instead of <StackPanel Orientation="Horizontal" .../> in ItemsPanelTemplate
Use a WrapPanel instead of a StackPanel. It doesn't allow you to directly specify the number of items per row, but you can set the width of each item, which is almost as good. When there is no space left on a row, it continues on the next row.
EDIT: you could also use a UniformGrid, as suggested by Bonial. The drawback is that if you can resize your UI and make the ListView wider, the number of items per row won't change, and they will be stretched to fill the space. Depending on what you want, it might be OK, but I think WrapPanel is a better option in most cases.
You want to replace that ListView.ItemsPanel StackPanel with a WrapPanel. It will get the job done. Once the WrapPanel has a width, it will then line wrap the items.

Listbox extending off the page

I have a listbox inside of a stack panel inside of a border in a silverlight application and when ever I add anything to the listbox it increases in height so the scrollbar is never used and it extends beyond the boundaries of the border element. I have tried explicitly setting the height attribute of the listbox, the border and the stack panel and it still goes beyond this.
Here is my code:
<Border x:Name="articlePane">
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button Content="Latest" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Pending" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Done" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
</StackPanel>
<ListBox x:Name="articleList" Margin="5" Background="Transparent" ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Center">
<Image x:Name="articleImage" />
<TextBlock x:Name="articleTitle" Text="{Binding Path=Title}" FontSize="18" FontWeight="Bold"
Margin="5"/>
<TextBlock x:Name="articleDate" Text="{Binding Path=Date}" FontSize="14" Foreground="Gray"
Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
I have a feeling that the StackPanel just keeps on adding height forever. I think in this instance you'd be better off with a Grid layout within your Border. Put the Button StackPanel in one height defined row, and the ListBox in an autosizing * height row. This way Grid will stick within the confines of your form.

Categories

Resources