WPF WrapPanel / ItemsControl not scrolling - wpf

I have a wrap panel displaying items but I cant get a scroll bar to work properly any idea's whats wrong ?
<ScrollViewer>
<ItemsControl Name="itemsControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel ItemWidth="{Binding ElementName=sizeSlider, Path=Value}"
FlowDirection="LeftToRight" Height="auto" Width="auto"
HorizontalAlignment="Left" Name="wrapPanel1"
VerticalAlignment="Top"
Margin="5"
>
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</StackPanel>

Your ScrollViewer is inside StackPanel which resizes to its content (either vertically or horizontally depending on its orientation). Place it either directly in a Window, a cell of a Grid, or a DockPanel for scroll bars to show up.

Related

Scrollbar of Scrollviewer only appears after resizing form or using GridSplitter

I have the problem, that the vertical scroll bar doesn't appear after application start.
I fill the ListPrinterButton in code behind and set the propertyChanged event. All button are visible in the ItemsControl but the vertical scroll bar only appears, after I resize this area by moving the gridsplitter.
Is there any option to "refresh" the ScrollViewer? I even tried to set VerticalScrollBarVisibility to Visible, but then, the scroll bar is visible, but not editable. Only moving the grid splitter or resizing the form will appear the right opportunity to scroll.
Can you please help?
<ScrollViewer Grid.Row="0" x:Name="svDeviceButton" VerticalScrollBarVisibility="Auto" PanningMode="Both" Background="Transparent">
<Grid>
<ItemsControl ItemsSource="{Binding ListPrinterButton, UpdateSourceTrigger=PropertyChanged}" x:Name="icPrinter">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal">
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</ScrollViewer>
<GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" x:Name="gsDeviceButton"/>
Thanks a lot in advance!

VirtualizingStackPanel bottom docking

I'm working on some chat application and using VirtualizingStackPanel. When my message list is scrolled down, I want it to be docked to the bottom of the container. But when I use virtualization there is always some whitespace, because panel docks top message, not the bottom. Here is an example: left is what I have and right is what I want.
Here is my markup.
<ItemsControl ItemsSource="{Binding Chat.MessagesList}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:ChatMessage}">
...
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer CanContentScroll="True"
PanningMode="VerticalOnly"
VerticalScrollBarVisibility="Auto"
VirtualizingStackPanel.IsVirtualizing="True"
VerticalAlignment="Bottom"
VerticalContentAlignment ="Bottom">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
Is there any possibility to have messages aligned bottom and save virtualization? Any clue would be helpful.
Add
VirtualizingPanel.ScrollUnit="Pixel" VirtualizingPanel.IsContainerVirtualizable="True" to the ItemsControl.
Scrolling will be measured by pixels instead of items in the collection.

WPF Group Style is disturbing wrap panel wrapping

I am using WrapPanel (with vertical orientation) as items panel for ItemsControl. Items are properly warpping without adding a group style. When i add a group style for items control then wrappanel is not wrapping items, all items are getting displayed vertically in a single column instead of spanning in to multiple columns. I am not using any complex layout for both group header or for ItemTemplate.
Here is the XAML source..
<Grid>
<ItemsControl ItemsSource="{Binding Items}" Grid.Row="0" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"
MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=ActualHeight}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"/>
<TextBox Text="{Binding Value}" Grid.Column="1" HorizontalAlignment="Right"></TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Finally figure out the problem.
When using GroupStyle we need to mention GroupStyle.Panel otherwise default panel will be used
which is a stackpanel.
The ItemsPanel which have declared as Wrappanel will not be used as main panel for group items layout.
<GroupStyle.Panel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"></WrapPanel>
</ItemsPanelTemplate>
</GroupStyle.Panel>
When you setup the GroupDescription as explained here, your items will be grouped into GroupItem collection and each group will be added as child to a Vertical StackPanel. The items inside a GroupItem will still use the WrapPanel you have defined as ItemsPanel, but since your WrapPanel's Orientation is set to Vertical and you are a descendant of a Vertical StackPanel (which gives Infinite vertical available space) the layout system does not wrap the items since it has all the space it needs to display the Items in a vertical manner.
Hope it made it clearer now and you can adapt your solution, maybe change the WrapPanel's Orientation to Horizontal.
below code (xaml) works in my case:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

Using a WrapPanel in a ListBox in Silverlight

I have a Silverlight application that has a ListBox of a specific width. I am dynamically adding items to this ListBox in my code-behind. If the items require more space than is allotted, I would like the items to wrap to the next line AND the height of the ListBox to increase. As it runs now, a horizontal scroll bar appears in my ListBox. In addition, no wrapping, thus no growing happens. What am I doing wrong? Here is my ListBox:
<ListBox x:Name="myListBox" Grid.Column="1" Width="600" MinHeight="24">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Thank you for your help!
Try setting ScrollViewer.HorizontalScrollBarVisibility to Disabled on the ListBox to prevent the horizontal scrolling and force the wraping.
<ListBox x:Name="myListBox" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Column="1" Width="600" MinHeight="24">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>

WPF: ListBox with WrapPanel, vertical scrolling problem

I have a UserControl (XAML below) that has a ListBox that I want to display images inside a WrapPanel, where images are displayed as many as will fit on one row and then wrap onto the next row etc. It works, except when the ListBox grows higher than the available space in the window, I'm not getting a vertical scrollbar, i.e. the contents get clipped. If I set a fixed height on the ListBox, the scrollbar appears and works as expected. How can I get this listbox to grow to the available space and then show a vertical scrollbar? This control is inside StackPanel inside a Grid in the main window. If I wrap the StackPanel inside a ScrollViewer, I get the scrollbar I'm after, but that's not really a good solution if I wanted to add some more controls to the UserControl above the ListBox (e.g. image size "zoom" etc) as I wouldn't want them to scroll with the images.
Thanks!! :)
<UserControl x:Class="GalleryAdmin.UI.GalleryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="LightGray" Margin="5" >
<StackPanel Margin="5">
<Image Source="{Binding Path=LocalThumbPath}" Height="100" />
<TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
I think you better go with override the ItemPanelTemplate:
<Grid>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>listbox item 1</ListBoxItem>
<ListBoxItem>listbox item 2</ListBoxItem>
<ListBoxItem>listbox item 3</ListBoxItem>
<ListBoxItem>listbox item 4</ListBoxItem>
<ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>
Well, I finally stumbled upon the solution. I was adding my UserControl to a placeholder panel that looked like this:
<ScrollViewer Margin="20" >
<StackPanel Name="contentPanel"></StackPanel>
</ScrollViewer>
However, when I switched it to a Grid instead, things started to work the way I wanted:
<Grid Name="contentPanel" Margin="20" />
I think it has to do with the StackPanel not taking up all the vertical space by default, like the Grid is doing.
All I had to do was set the following, and the problem went away:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
I was just looking through several questions about this issue, and although this is an old thread, this one gave me the answer, but just to clarify....
The layout GRID is the answer to most issues like this. To get the proper ListBox/WrapPanel operation to fill the available space, the following code does the trick:
<Grid Grid.Row="1" MaxHeight="105">
<ListBox ItemTemplate="{DynamicResource StoreGroupTemplate01}" ItemsSource="{Binding StoreGroupHeader}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
I have this in another grid to place the list at the bottom of my screen (ie.. the Grid.Row="1") and you can adjust MaxHeight (or remove it) to control the visible area before the vertical scroll bar will show up.
Put your listbox inside of a ScrollViewer and then set the scrollviewer's VerticalScrollBarVisibility property to "Auto"
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="LightGray" Margin="5" >
<StackPanel Margin="5">
<Image Source="{Binding Path=LocalThumbPath}" Height="100" />
<TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</ScrollViewer>
HTH

Resources