基于DateTime创建自定义GroupDescription [英] Creating custom GroupDescription based on DateTime
问题描述
我正在对一些数据进行分组,并且PropertyGroupDescription在大多数情况下都能正常工作。但是,如果该属性是DateTime,而我又不想将多个日期作为一个组进行分组(例如每组30天左右),则需要一个新的GroupDescription。问题是我不知道该类实际上是如何工作的,以及如何设计这样的类。
I'm grouping some data and PropertyGroupDescription works fine most of the time. However if that property is a DateTime and i wan't to group several dates together as one group (like 30 days in each group or something) I would need a new GroupDescription. Problem is I have no idea how the class actually works and how I would design such a class.
我希望能够继承PropertyGroupDescription(而不是基本的抽象类),因为这也将基于属性,但是在这里,我基于一系列值而不是单个值== 1组进行分组。
I'm hoping to be able to inherit PropertyGroupDescription (instead of the basic abstract class) because this will also be based on a property but here I'm grouping based on a range of values instead of a single value == 1 group.
任何指南甚至是这样的现成课程?
Any guide or even a ready class like this?
推荐答案
有点晚了,但是正如您所说的那样 IValueConverter
可以用于此-这是我使用过的一个简单转换器,它将按友好的相对日期字符串分组:
A bit late, but as you say yourself IValueConverter
can be used for this - here's a simple converter I used once that will group by a friendly relative date string:
public class RelativeDateValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var v = value as DateTime?;
if(v == null) {
return value;
}
return Convert(v.Value);
}
public static string Convert(DateTime v)
{
var d = v.Date;
var today = DateTime.Today;
var diff = today - d;
if(diff.Days == 0) {
return "Today";
}
if(diff.Days == 1) {
return "Yesterday";
}
if(diff.Days < 7) {
return d.DayOfWeek.ToString();
}
if(diff.Days < 14) {
return "Last week";
}
if(d.Year == today.Year && d.Month == today.Month) {
return "This month";
}
var lastMonth = today.AddMonths(-1);
if(d.Year == lastMonth.Year && d.Month == lastMonth.Month) {
return "Last month";
}
if(d.Year == today.Year) {
return "This year";
}
return d.Year.ToString(culture);
}
public static int Compare(DateTime a, DateTime b)
{
return Convert(a) == Convert(b) ? 0 : a.CompareTo(b);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后可以像这样使用它:
You can then use it like this:
view.GroupDescriptions.Add(
new PropertyGroupDescription("Property",
new RelativeDateValueConverter()));
这篇关于基于DateTime创建自定义GroupDescription的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!