0

Is it possible to have empty rows displayed inside of a listbox/listview in wpf? I've been trying to replicate a repeating pattern of rows after my data so the listbox looks "full" but I haven't managed to figure out how to do this as of yet. The problem is the listbox will only show if there is any data in the observablecollection its bound to, so if there is no data the listbox wont render anything, and even if there is data, it'll only show the amount of rows the observablecollection is holding and nothing below the rows. I want to be able to have empty rows appearing in the listbox after the data, or a repeating pattern of empty rows if at all possible. Any help or tips would be appreciated.

1 Answer 1

1

There are several ways to achieve this result: 1) Add more (empty) rows to your collection 2) Use a separate collection that wraps your collection, and contains the empty rows (this is useful if you don't want to pollute your source with extra data) 3) Create a custom panel to use (instead of StackPanel, WrapPanel, etc) that your ItemsControl can use to display the items - then the panel itself handles displaying the empty rows. This a more robust and less hacky solution than 1 and 2, but requires more effort and WPF knowledge.
4) Create a subclass of ItemsControl (or ListBox/ListView) that automatically adds extra Items to fill the remaining space (similar to (3), but instead of a custom panel plugged into the ListBox, you are customizing the ListBox to create extra items for any panel to display. This is useful if you want to reuse the logic in different contexts where the consumer might want to vary the Panel type used.

Sign up to request clarification or add additional context in comments.

2 Comments

I was actually doing the first approach (adding empty rows), the only problem is that i wasn't sure how to control how many rows to add as I don't want a scrollbar appearing or else it would kill the illusion of the repeating pattern. However if there was actual data that would require a scrollbar, i would obviously want one to appear.
In that case you'll have to do the math, based on the height of your listbox (which could change depending on the layout), and the height of the individual items. You should be able to access both of these values dynamically, and update the datasource at that time, although finding the best events to use could be tricky. In that situation it might be easier to just create a custom panel, since the measure/layout events that are triggered automatically are already in place and you'd just need to do the calculation and add the items (in this case to the control, rather than the datasource).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.