0

I have created a CollectionViewSource as follows:

<CollectionViewSource x:Key="MyGrouping" Source="{Binding MyCollection}">
    <CollectionViewSource.SortDescriptions>
        <componentModel:SortDescription PropertyName="Name"/>
    </CollectionViewSource.SortDescriptions>
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Type"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

I then define a TabControl as follows:

<TabControl ItemsSource="{Binding Groups, Source={StaticResource MyGrouping}}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
                <DataGrid.Resources>

.....

All of the grouping works perfectly. However, I wanted to add an additional Tab that contains ALL of the items (ungrouped).

Is there a simple mvvm approach to doing this? Any help would be greatly appreciated.

gleng
  • 6,185
  • 4
  • 21
  • 35

1 Answers1

1

A basic idea is to create custom ListCollectionView with customized CollectionViewGroup. Below is the example.

public class CustomGroupListCollectionView : ListCollectionView
{
    private readonly CustomGroup _allGroup;

    public CustomGroupListCollectionView(IList list)
        : base(list)
    {
        _allGroup = new CustomGroup("All");
        foreach (var item in list)
        {
            _allGroup.AddItem(item);
        }
    }

    public override ReadOnlyObservableCollection<object> Groups
    {
        get
        {
            var group = new ObservableCollection<object>(base.Groups.ToList());
            group.Add(_allGroup);
            return new ReadOnlyObservableCollection<object>(group);
        }
    }
}

public class CustomGroup : CollectionViewGroup
{
    public CustomGroup(object name)
        : base(name)
    {
    }

    public void AddItem(object item)
    {
        ProtectedItems.Add(item);
    }

    public override bool IsBottomLevel
    {
        get { return true; }
    }
}
Bill Zhang
  • 1,909
  • 13
  • 10
  • Bill, how would you get the underlying sorting (from the xaml) and apply it to the new grouping (all items)? – gleng Jul 11 '13 at 12:57
  • Aha, this is what I mean a basic idea. When you want to add more functionality, such as sorting and filtering, more pain will come. I have not done that. You can consider listen to SortDescriptions's CollectionChanged event, then sort _allGroup manually in the event handler. – Bill Zhang Jul 11 '13 at 20:47