tags:

views:

268

answers:

4

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.

A: 

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.

MusiGenesis
I will look at the listview. We cannot pre-size the listbox. Its height will grow when/if the user resizes the window.
David Martin
Good on you David for handling resize properly! Not doing so is a major pet peeve of mine.
Scott J
Actually, my real answer should have been: "don't bother". What you're describing is normal listbox behavior; I doubt whether you would lose any customers because of it.
MusiGenesis
Our listbox items are interactive (they have numeric up/down spinner buttons). When there is an item that is 90% shown at the bottom the user attempts to click the up/down button and WPF kindly repositions that item. Now their mouse is over the next item. If they aren't aware they will be editing the wrong item.
David Martin
@David: that makes total sense. I apologize for engaging in the standard StackOverflow behavior of presuming I know better than the person asking the question. With what you're describing, I probably *would* use a custom control that either wraps or inherits from Panel.
MusiGenesis
Although I do have to mention how much I hate spinner buttons. :)
MusiGenesis
Actually I have been fighting this as well and in my case it is simply double clicking the partial item. The first click causes the listbox to scroll and the 2nd (The new partial) gets the double-click event.
Mike B
+1  A: 

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.

Mike B
Can you give a bit more detail to this answer?
Roger Lipscombe
I will try to throw up a simple sample this weekend
Mike B
+1  A: 

you can achieve this by finding the actual heights of the content of the listbox in dispather and adjust the listbox's height, but, it will make the listbox size either grow or shrink depends on the implementation.

Avatar
A: 

My vote is a custom panel for each item. This panel does not display if it cannot display itself completely. The list can resize as needed because its only job is to be a resizable container that provides an area for the panels. Panels can grow and shrink as needed.

AMissico