开发者

Dynamically grouping/nesting flat data in a TreeView

I would like to take a flat list of objects and present them in a TreeView using custom groups.

public enum DocumentType { Current, Inactive, Transition, Checkpack, TechLog, Delivery }
public enum Status { Approved, Rejected, Pending }

public class Document
{
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime Modified { get; set; }
    public string ModifiedBy { get; set; }
    public DocumentType Type { get; set; }
    public Status Status { get; set; }
}

For example... The user might want to see this list, with the top level group being "Status" and the second level being "Name". This all needs to be configurable from the UI, and I'm struggling to find the best way to achieve it.

I've had a brief look at the CollectionViewSource object, but couldn't find a good way to get it to dynamically build a TreeView.

My gut feeling is that i'll need to do some clever templating in XAML - this is as far as i've got...

<Window.Resources>
    <DataTemplate x:Key="DocumentTemplate">
        <DockPanel>
            <TextBlock Text="{Binding Name}" />
        </DockPanel>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="GroupTemplate"
                              ItemsSource="{Binding Path=Items}"
                              ItemTemplate="{StaticResource DocumentTemplate}">
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <TreeView ItemsSource="{Binding Documents.View.Groups}"
              ItemTemplate="{StaticResource GroupTemplate}"/>
</Grid>


    public CollectionViewSource Documents
    {
        get
        {
            var docs = new CollectionViewSource();
            docs.Source = DocumentFactory.Documents;
            docs.GroupDescriptions.Add(new PropertyGroupDescription("CreatedBy"));
            return docs;
        }
    }

Of course this only displays the Top-level group ("CreatedBy").


After reading a question below, I managed to come up with a better question...

My question: Is it possible to have a generic HierarchicalDataTempla开发者_开发百科te for a TreeView that displays custom groups applied to a CollectionViewSource.


Honestly this should be marked as a bug in WPF. I too tried and found that Documents.View.Groups throws binding error on View property being null.

Also

 <TextBlock Text="{Binding Path=Name}" />

is correct in the GroupTemplate but not in the DocumentTemplate. Note that Groups are of special type GroupItem where Name is one such property that holds the value on which grouping has taken place.

On the other hand in DocumentTemplate, we should refer the property that we need to display on the leaf nodes items e.g. in my example I used Employee.FirstName (I grouped on Gender).

 <DataTemplate x:Key="DocumentTemplate">
      <DockPanel>
          <TextBlock Text="{Binding FirstName}" />
      </DockPanel>
 </DataTemplate>

Now for binding to take effect I had to introduce a converter which simply returns Groups.

public class GroupsConverter : IValueConverter
{
    public object Convert(object value, ...)
    {
        return ((CollectionViewSource)value).View.Groups;
    }

    ....
}

And tree view binding was changed this way...

<TreeView x:Name="treeView"                  
          ItemsSource="{Binding Path=Documents,
                                Converter={StaticResource GroupsConverter}}"
          ItemTemplate="{StaticResource GroupTemplate}" />

Then this worked for me.

Does this help you?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜