EDIT #2:
Here's a minimal Github Repo I created to reproduce the problem.
I believe this is a bug in the implementation of virtualization of a grouped ListViewBase when some groups are empty
I have a listview whose items panel is set to StackPanel since I need to set the group style panel to a staggered panel which doesn't work well with item stack panel.
The problem now is, out of 27 groups, only 16 groups are shown. I tested it on another list view and I confirmed that the problem is that the stack panel only displays a limited number of children (the strange thing is it's not 16 on my test but 22).
I tried VirtualizingStackPanel but unlike StackPanel, I can't seem to center it inside the list view upon setting its MaxWidth though it displays all its children.
Here's my XAML:
<ListView ItemsSource="{Binding Source={StaticResource NoteViewSource}}"
Padding="0 0"
ItemTemplate="{StaticResource NormalNoteTemplate}"
x:Name="ListView"
IsItemClickEnabled="True"
ItemClick="ListView_OnItemClick"
SelectionChanged="ListView_OnSelectionChanged"
SelectionMode="Extended">
<ListView.Footer>
<Border Height="80" />
</ListView.Footer>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="False">
<GroupStyle.HeaderTemplate>
<DataTemplate x:DataType="viewModels:NoteList">
<TextBlock Text="{x:Bind Header, Mode=OneWay}"
FontSize="18"
Margin="0,16,0,8"
FontWeight="Bold" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<uwp:SGStaggeredPanel DesiredColumnWidth="220"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!-- custom group panel won't work if this is item panel-->
<StackPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Edit:
I inspected it further and it seems that the StackPanel is not the problem since its Children count is 22. This means it has the wrong count of children which means the problem is in the CollectionViewSource or the ListView passing only 22 items in the StackPanel rather than the full count.
But what I can't understand is why does the VirtualizingStackPanel work??
Instead of using the StackPanel, consider using the ItemsStackPanel. It's designed to work with groups.
<ItemsPanelTemplate>
<ItemsStackPanel />
</ItemsPanelTemplate>
From Microsoft docs:
ItemsStackPanel: Arranges child elements of an ItemsControl into a single line that can be oriented horizontally or vertically. Supports pixel-based UI virtualization and grouped layouts.*
Related
Im trying to make a custom selector that fills the whole page and looks like this (each box represents different selection):
The elements have to be loaded from xml, allways 3 per row, infinite amount of rows possible (based on xml file).
So far i have gotten to building custom DataTemplate with this example:
example
But i have no idea how to approach making selection show fullscreen with multiple rows.
Since you have a fixed number of columns you can use an ItemsControl or a ListBox using a UniformGrid as the ItemPanel used to lay out the items
Sample Xaml Template
<ListBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ItemsSource="{Binding MyElementsLoadedFromXml}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="26" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding AFieldInMyXmlElement}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
It should look like this by default:
The IsItemsHost="True" is optional when not using a scrollviewer but as it is meant to be used in this use case I think it is better to use it here anyway
Edit: I forgot it was about ComboBox but as they behave really close to each other, if you replace ListBox by ComboBox you get what I think is your expected result :
I am looking for a custom ListView control which arranges the items horizontally and if the max width is reached, add the item to the next row.
The WrapGrid as ListView.ItemsPanel doesn't work because it uses equal width for each item (grid structure).
The WrapPanel from the WinRT XAML Toolkit has no ItemTemplate and I cannot use binding due to missing ItemSource property.
Could I write my own ListView implementation with wrapping? Which methods do I need to override?
The solution is to use the WrapPanel from WinRT XAML Toolkit inside the ListViews ItemsPanel:
<ListView x:Name="Keywords" SelectionMode="Multiple" ItemContainerStyle="{ThemeResource ListViewItemStyle}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
I am having some issues in my Windows 8.1 app regarding getting a nested ListView to scroll properly. Basically I have the following scenario:
I have a page that needs to display a list of 'packages' in a vertical ListView. Each of those packages then has 1-15 images inside of it that I would then like to display in a horizontal ListView. Basically this means I have a list of lists that I would like to display (display the main list vertically, and for each list in the vertical list, list the images horizontally).
I have this almost working, but when it comes to trying to scroll through the nested ListView of images, it is very difficult because the scroll bar/area is right below the images and most of the time you end up touching the image and "selecting" it instead. I'd like to be able to swipe to scroll but I can't seem to get that to work either.
Is there a better way to approach getting a nested horizontal listview to scroll properly? I think one of the issue may be that my touch events aren't being routed the way I want them to be.
At this point my XAML for this looks like the following:
<ScrollViewer x:Name="OuterScrollView">
<ListView x:Name="ImageRoot" Height="851" Width="656" DataContext="{StaticResource GlobalDataContext}" ItemsSource="{Binding MainImageInfo.ImagePackages}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding PackageName}"/>
<ScrollViewer>
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageBitmap}" Height="200" Width="200"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
Any feedback is appreciated.
I have the following code in a Metro app. The listview binds up to a list of objects. My problem is that the WrapGrid assigns them equal width. But some Titles are longer then others so width should be set width to Auto. But this dont work.. anyone have any idea?
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
This works for me:
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid ItemWidth="250" ItemHeight="80" MaximumRowsOrColumns="7" VerticalAlignment="Center" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
The key point here is MaximumRowsOrColumns property. Have fun :)
I think it's not the WrapGrid, but the ListView itself through the ListViewItem/ItemContainerStyle that makes them same size. I don't think there really is a workaround for that though. You could use your own ItemsControl and custom panel or calculate the desired size of each item and based on that - put the items in something like a VariableSizedWrapGrid, setting RowSpan/ColumnSpan appropriately to match that desired size. Other than that - you can either make all items wide enough to fit everything or simply clip the content and display the full strings in the detailed view once the user clicks an item.
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.