I have a Windows 8.1 application with a GridView
bound to a custom (sortable, deduplicated) observable collection. In this collection, I do some heavy filtering and setting an IsHidden flag for every item.
In the data template for the item, there is a condition making the item collapsed if IsHidden flag is set to true.
<Grid Width="160" Height="280" Visibility="{Binding IsHidden, Converter={StaticResource InvertedBooleanToVisibilityConverter}}">
This approach works in Windows Phone 8.1 XAML, making the items disappear from the ListView
but it does not work in Windows 8.1 GridView
. The problem with Windows 8.1 is that when I set an item in the collection to hidden, id disappears from the GridView
but leaves an empty place, so there is a gap in the GridView
.
Any ideas on how to solve it? Maybe same XAML style editing?
Here is a minimal solution to reproduce the problem: https://dl.dropboxusercontent.com/u/73642/gv.zip
I tried binding width and height of the items to the hidden flag and setting it to 0 when the item is hidden, but it did not help, still a gap in the GridView
.
Update: One workaround would be filtering the actual bound collection, but this is not possible, because of some business requirements.
It takes me a lot of time to understand the problem, and the solution right in front of my eyes. You trying to hide the item itself but the container still there, When you add an item to an GridView, the item is wrapped in an item container. from msdn :
You need to disable the container and create two DataTemplate and using DataTemplateSelector you can choose which DataTemplate for disable and active items. Check this useful article .
I tried your sample solution and changed it to a ListView instead. It exhibits the same behavior when the grid itself is hidden. I don't have XAML Spy to verify, but it appears that any List based control will allocate a rendered item for each item in the list.
I changed your click handlder to instead
ds.RemoveAt(5);
instead of hiding the item, and the element is removed from view with a nice animation. This appears to be as expected, and an interesting find.The problem is in the
GridView
'sItemsPanel
.Both
ItemsWrapGrid
andWrapGrid
are uniform grids. All their child elements will be sharing the same height and width. That's why even if you collapse theItemTemplate
, the space is still reserved.What you really need here is a
WrapPanel
. WINRT doesn't have a built-inWrapPanel
but Jerry Nixon has built one and you can grab it from here.After you updated your
GridView
sItemsPanel
, you still have one more thing to do. You need to also get theGridViewItem
that hosts yourItemtemplate
and set itsVisibility
toCollapsed
.I put a little delay above to make the collapsing more obvious.