开发者

Can an IGrouping enumeration be bound in XAML? And if so, what would the binding syntax look like?

开发者 https://www.devze.com 2023-01-04 16:45 出处:网络
I have a list of Customer objects, which I\'ve grouped on Country. var query = from c in ctx.Customers group c by c.Country

I have a list of Customer objects, which I've grouped on Country.

var query = 
    from c in ctx.Customers group c by c.Country
    ...

query is now an enumeration of groups (IQueryable<IGrouping<string, Customer>>), where each item of thie enumeration defines a group (IGrouping<string, Customer>).

I understand that IGrouping is: the key of the group (Country in my example) and the items grouped by Country. It's straightforward how to iterate through this data with a foreach statement, but what I need help on is how to bind the grouping in XAML.

I would like to use nested <ItemsControl> controls, the outer one to list the Country names and an inner one to iterate through the grouped customers for that country. By problem lies with binding to an IGrouping group.

Can it be done and if so, would 开发者_开发问答the binding syntax look like?


You can use a HierarchicalDataTemplate. For instance, if you want to bind a TreeView to the result, you can do something like that :

<TreeView ItemsSource="{Binding Groups}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding}">
            <TextBlock Text="{Binding Key}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
0

精彩评论

暂无评论...
验证码 换一张
取 消