0
votes

I have the following class:

public class BankingEntry
{
    private DateTime entryDate;
    public DateTime EntryDate { get { return entryDate; } }
}

I want to bind an ObservableCollection< BankingEntry> object to the ItemsSource of a WPF TreeView and have the items display in the following tree structure:

---Year

------Month

---------Day

So if my collection had 3 items with entry dates of 2012-12-30, 2012-12-31 and 2013-01-01 if would get the following tree view structure:

---2012

------12

---------30

---------31

---2013

------01

---------01

Ideally I would like the tree view to automatically update when an item is added or removed from the collection. The dates of the objects will never change so I don't have to worry about updates.

Thanks in advance

2

2 Answers

1
votes

Somewhat old question but had the same problem and here's what I've ended up doing

Created a IHierarchy interface

interface IHierarchy<T>
{
    T Value { get; set; }

    IEnumerable<IHierarchy<T>> Children { get; set; }

    int Level { get; set; }
}

Then an implementation of Hierarchy for dates

private class DateHierarchy : IHierarchy<DateTime>
{
    public DateTime Value { get; set; }

    public IEnumerable<IHierarchy<DateTime>> Children { get; set; }

    public int Level { get; set; }
}

A method to build the dates from a list of dates

private void BuildDates()
{
    var start = new DateTime(2010, 01, 01);

    var end = new DateTime(2015, 12, 31);

    var dates = new List<DateTime>();

    for (var date = start; date <= end; date = date.AddDays(1))
    {
        dates.Add(date);
    }

    var d = from date in dates
            group date by date.Year into year
            select new DateHierarchy
            {
                Level = 1,
                Value = new DateTime(year.Key, 1, 1),
                Children = from date in year
                            group date by date.Month into month
                            select new DateHierarchy
                            {
                                Level = 2,
                                Value = new DateTime(year.Key, month.Key, 1),
                                Children = from day in month
                                            select new DateHierarchy
                                            {
                                                Level = 3,
                                                Value = day,
                                                Children = null
                                            }
                            }
            };

    Dates = new ObservableCollection<IHierarchy<DateTime>>(d);
}

and finally in xaml

<TreeView ItemsSource="{Binding Dates}"
          SelectedItemChanged="TreeView_SelectedItemChanged">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock x:Name="Date" />
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Level}" Value="1">
                    <Setter TargetName="Date" Property="Text" Value="{Binding Value, StringFormat=yyyy}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Level}" Value="2">
                    <Setter TargetName="Date" Property="Text" Value="{Binding Value, StringFormat=MMMM}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Level}" Value="3">
                    <Setter TargetName="Date" Property="Text" Value="{Binding Value, StringFormat=dd/MM/yy}" />
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
1
votes

The answer above from T I is great and saved me a lot of time. Posting an update here for anyone who may need this in method syntax, as I did, instead of query syntax.

var d = dates.GroupBy(date => date.Year)
            .Select(year => new DateHierarchy
            {
                Level = 1,
                Value = new DateTime(year.Key, 1, 1),
                Children = year.GroupBy(date => date.Month)
                    .Select(month => new DateHierarchy
                    {
                        Level = 2,
                        Value = new DateTime(year.Key, month.Key, 1),
                        Children = month.GroupBy(date => date.Day)
                            .Select(day => new DateHierarchy
                            {
                                Level = 3,
                                Value = new DateTime(year.Key, month.Key, day.Key),
                                Children = null
                            })
                    })
            });

I also needed to group items by the day of the week. To do this you can use CultureInfo.CurrentCulture.Calendar.GetWeekOfYear() as well as ISOWeek.ToDateTime() as follows.

var d = dates.GroupBy(date => date.Year)
            .Select(year => new DateHierarchy
            {
                Level = 1,
                Value = new DateTime(year.Key, 1, 1),
                Children = year.GroupBy(date => date.Month)
                    .Select(month => new DateHierarchy
                    {
                        Level = 2,
                        Value = new DateTime(year.Key, month.Key, 1),
                        Children = month.GroupBy(date => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday))
                            .Select(week => new DateHierarchy
                            {
                                Level = 3,
                                Value = ISOWeek.ToDateTime(year.Key, week.Key, DayOfWeek.Sunday),
                                WeekOfYear = week.Key,
                                Children = week.GroupBy(date => date.Day)
                                    .Select(day => new DateHierarchy
                                    {
                                        Level = 4,
                                        Value = new DateTime(year.Key, month.Key, day.Key),
                                        Children = null
                                    })
                            })
                    })
            });

Now to build and display that properly you will need to add a WeekOfYear property in your DateHierarchy class as well as add the appropriate DataTrigger in your xaml