In my Metro application, I have a data source containing a certain number of items (say 25). I have a ListView that presents those items. My problem is that the ListView have a size that allows it to display, say, 6.5 items, so that the last item it displays is cut in half. If the resolution changes, it might display 4 items, or 8.2 items, or whatever. What I'd like is that the ListView shows exactly the number of items that fits in the height of the control, instead of clipping the last item.
Right now, I see two possible half-solutions, none of which is optimal:
Set the height of the ListView to a fixed height that is a multiple of the item size. This does not scale with changes in resolution.
Limit the number of items in the data source. This does not scale either.
So my question is, how can I get the ListView to only display complete items (items where all edges are inside the viewport/listview), and hide the rest?
Thanks in advance!
The answer from @JesuX is the better approach -- if done correctly. The following
ListView
subclass works fine for me:One caveat -- if you plop an
IntegralItemsListView
into aGrid
, it will haveby default, which defeats the purpose of this class.
Also: If the items are of uniform height, the method can obviously be simplified:
My final solution was to combine the suggestions of @NovitchiS and @JesuX.
I created a stack panel override, and listened to the LayoutUpdated event. My final solution:
ListView inherits from ItemsControl, so one more optimized solution consists in injecting custom panel (overriding measure by custom clipping display) in ItemsPanel
something like this(sorry, i did not try to compile):