views:

31

answers:

2

Hi,

I wanna create a WPF treeview with data with two grouping options (radio buttons). So the data will be grouped in two different ways (2 level hierarchy), the lowest level being the actual data items, and the groups being just a way to represent the data for easier understanding. They would also be able to select items by group (checkboxes) but I got that part already figured out. E.g. if I want to represent database objects and want to have them grouped either by schema or by object type (table, view, function,...).

I just don't know how I should start on the two grouping modes. Should I entirely restructure my ObservableCollection whenever the grouping mode changes or is there a more straightforward way? Also, what if my DataTemplate for the 2-nd level would be slightly different depending on the grouping mode (e.g. if grouped by object type you need to display the schema on level 2)?

Can anyone give me some tips on how to start and which techniques to use?

Thanks

A: 

Check this link on HierarchicalDataTemplate. There are given examples of using it with various types.

Eugene Cheverda
That would be indeed the way I'd do it if I choose to restructure my whole ObservableCollection. I just feel that there should be a more convenient technique for this, like with LINQ queries or something...
Koen
A: 

Group your collection by setting GroupDescriptions on its CollectionViewSource. You can do it in code by doing something like this:

CollectionViewSource.GetDefaultView(yourCollection).GroupDescriptions.Add(
    new PropertyGroupDescription("PropertyName"));

Or you can do it in XAML by creating a CollectionViewSource explicitly.

    <CollectionViewSource
        Source="{StaticResource yourCollection}"
        xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework">
        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="PropertyName"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

If you are using a plain ItemsControl like ListBox then you can just set the GroupStyle property. If you want to use a TreeView, then I think you want to bind to the Groups property on the ICollectionView. You should read Bea Stollnitz's blog entries on grouping:

Quartermeister
Interesting but I'm not sure if I can use this. I need checkboxes in front of my items on both levels and checking a group should check all child items, etc. I did that now with code in my model.Also, if there's a way around that, I would still need to make PropertyName vary on the selection of radio buttons, which I believe cannot be done because it is not a DependencyProperty...
Koen
@Koen: You could bind check boxes to the CollectionViewGroup with a custom converter that checks each child. For changing the grouping, you could do it in code, either in the code-behind or in your ViewModel, or you could just have two CollectionViewSources that are grouped in different ways and change which one you bind the TreeView to.
Quartermeister