我有以下课程:
public class BankingEntry
{
private DateTime entryDate;
public DateTime EntryDate { get { return entryDate; } }
}
我想将ObservableCollection
-年
------月
---------天
因此,如果我的收藏集中有3个条目的条目日期为2012-12-30、2012-12-31和2013-01-01,则它将获得以下树形视图结构:
--- 2012
------ 12
--------- 30
--------- 31
--- 2013
------ 01
--------- 01
理想情况下,当从集合中添加或删除项目时,树状视图会自动更新。对象的日期永远不会改变,因此我不必担心更新。
提前感谢
有点旧的问题,但是有相同的问题,这是我最后要做的事情
创建了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>
上面我的答案很好,节省了我很多时间。和我一样,在此处为可能需要方法语法的人而不是查询语法发布更新。
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