0

我有以下课程:

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

我想将 ObservableCollection<BankingEntry> 对象绑定到 WPF TreeView 的 ItemsSource 并让项目显示在以下树结构中:

- -年

- - - 月

- - - - -天

因此,如果我的收藏有 3 个条目的条目日期分别为 2012-12-30、2012-12-31 和 2013-01-01,如果将获得以下树视图结构:

---2012

------12

---------30

---------31

---2013

------01

---------01

理想情况下,我希望树视图在从集合中添加或删除项目时自动更新。对象的日期永远不会改变,所以我不必担心更新。

提前致谢

4

2 回答 2

1

有点老的问题,但有同样的问题,这就是我最终做的

创建了一个 IHierarchy 界面

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

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

    int Level { get; set; }
}

然后是日期的层次结构的实现

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

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

    public int Level { get; set; }
}

一种从日期列表构建日期的方法

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);
}

最后在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>
于 2013-06-25T00:47:26.460 回答
1

TI 的上述答案很棒,为我节省了很多时间。像我一样,为任何可能需要方法语法而不是查询语法的人发布更新。

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
                            })
                    })
            });

我还需要按星期几对项目进行分组。为此,您可以使用CultureInfo.CurrentCulture.Calendar.GetWeekOfYear()以及ISOWeek.ToDateTime(),如下所示。

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
                                    })
                            })
                    })
            });

现在要正确构建和显示,您需要在DateHierarchy类中添加WeekOfYear属性,并在 xaml中添加适当的DataTrigger

于 2020-04-01T21:40:47.453 回答