Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not show partial items in a WPF listbox

Tags:

wpf

listbox

I've tried Google and I've tried Bing to no avail. Does anyone here have an idea on how to prevent partial items from appearing in a listbox in WPF? In case that does not make sense here is an example: Listbox is 200 pixels tall - each item is 35 pixels tall. That means I can show 5.7 items. 7/10 of an item is undesirable. I'd like to limit it to showing only 5 items. The user could then scroll to see the additional items.

Should I A) try to dynamically size the listbox or ScrollViewer ViewPort so that it fits perfectly? Or B) implement a custom panel that would not arrange a child whose desired height is more than the remaining vertical space?

Any thoughts would be greatly appreciated. Last note: If anyone knows of a 3rd party control (listbox or grid) that does this I would be interested in that as well.

like image 872
David Martin Avatar asked Mar 30 '10 15:03

David Martin


2 Answers

Just resize it in the designer until you don't get a partial row. Implementing a custom panel is way too much work for something like this.

You could also try a ListView instead of a ListBox. I may be misremembering this, but I think a ListView doesn't show partials, or maybe has an option to not show partial rows.

like image 86
MusiGenesis Avatar answered Sep 21 '22 03:09

MusiGenesis


I have been thinking on this today since my project has been fighting this issue. My thoughts are related to my project but should be applicable. I am assuming a MVVM ViewModel but it would work without it.

Bind a property to the height of the container that the ListBox is in then bind the height of the ListBox to that using a ValueConverter to cause the ListBox to expand or shrink in stages based on the height of a single item. This might look a little funny when resizing but with a brief animation could look fine.

like image 34
Mike B Avatar answered Sep 19 '22 03:09

Mike B