I'm using the default Grid app template and I had a question that I have not found a good answer to yet.

If you just use the sample data and remove all the data elements from a grouping, is there any way to show an item template that just says "No items" easily?

The group header is still present which is fine, just would be helpful to have an indicator there is no data present in the items list.

I tried a DataTemplateSelector but it isn't used if no items are present. It's a little baffling how something like a ListView or GridView doesn't have an EmptyDataTemplate

Edit The reason I'm asking about this particular scenario is that this application is showing employees grouped by companies. If you add a new company, I would still like the company's name to show, and if you click on the Header of the group, you could still see information about the company, even if no employees have been assigned.

Therefore my group headings should still be visible to navigate into, even if no employees (items) are assigned. I would like an item directly underneath the group header to say something like "No employees present" but still leave the company information visible.

So for instance

Company 1 >

  • Employee 1
  • Employee 2
  • Employee 3

Company 2 >

  • No employees present

Clicking Company 2 would still show you information about that company on the GroupDetails page.

有帮助吗?

解决方案

I managed to solve my problem with a combination of another question here, and by stubbing in an empty item in my group.

So I stubbed in a false employee with empty data, and then adjusted my DataTemplate. The trick was taking the object's binding container and disabling it for items that matched a specific criteria.

public class CompanyTemplateSelector : DataTemplateSelector
{
    protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
    {
        var containingItem = container as GridViewItem;

        if (item is UserDetails)
        {
            UserDetails detail = (UserDetails)item;
            if (detail.UserId == -1) 
            {
                containingItem.IsEnabled = false;
                return Empty;
            }
            else
            {
                containingItem.IsEnabled = true;
                return DataPresent;
            }
        }

        return DataPresent;
    }

    public DataTemplate DataPresent
    {
        get;
        set; 
    }

    public DataTemplate Empty
    {
        get;
        set; 
    }
}

So my result is the following

step1

and then after clicking the header

step2

其他提示

DataTemplateSelector is your best bet if you want to display a group header and show you have no items in the group. A better option might be to simply not show an empty group - with GroupStyle.HidesIfEmpty

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top