问题
My application have a ListView with GridLayout. I am now trying to introduce ListView groups into my application. Say, the data source would usually have Group1, Group2 and Group3. I would like to display all 3 groups all the time regardless there is element in it or not. When there is no element in a group, I want to display a "empty group" message under the group title.
I think the default way that WinRT
handles it is not to display the empty group and it makes a lot of sense in many scenarios. To do this, I know that I maybe able to add a dummy item to the list view when there is no data, but this is kind of hacky.
So, is there a better way to do this?
回答1:
Just bind your ListView to a collection of Group objects (where Group is a class you define and Group1, Group2 and Group3 are such Group objects).
In addition to Group level properties (such as a Title), have a Group contain a collection of Item objects.
In the ListView's datatemplate, use another ListView to show the Item elements for each Group.
Be careful though, the nesting of GridViews will result in nested ScrollViewers. You would want to remove the ScrollViewer from the inner GridViews by changing their control template.
来源:https://stackoverflow.com/questions/10503510/how-to-give-a-empty-listview-message-when-there-is-no-data-source