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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With