简体   繁体   中英

Grouping in WPF Datagrid?

So I have an ObservableCollection of objects (Each object has a Name, Number, and Type property):

Name Number Type
a    1      1
a    1      2
b    2      1
c    4      3
c    4      5

What I'd like to display in the datagrid is something like this instead (grouping by type):

Name Number Types
a    1      1, 2
b    2      1
c    4      3, 5

A bit confused on how to achieve this with Model View. One way is I could create a new object type, one that has a List<int> for Types, then iterate over the original collection and create a new collection that is bound instead to the DataGrid . Just wondering if there's a more preferred/easier way.

You can use a CollectionViewSource that wraps your current data source. This will allow you to add Groupings and Filters.

<CollectionViewSource x:Key="cvsTasks" Source="{StaticResource tasks}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="ProjectName"/>
        <PropertyGroupDescription PropertyName="Complete"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

See this link for complete examples: https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM