【发布时间】:2018-09-14 12:53:57
【问题描述】:
我有一个基于一组对象的可观察集合的 ViewSource,该集合基于一个名为“Ticket”的 C# 类,用于 C# .net 4 中的 WPF 项目的 DataGrid。该项目用于一个简单的帮助台应用程序。
在属性调用 TktDate 上分组的 ViewSource 以及当天输入的所有票证显然在我的 DataGrid 中分组在一起
但是我最近更新了代码,所以 TktDate 现在不仅存储日期元素,还存储时间元素,所以很明显,当我的意思是仅在日期上分组时,由于时间元素,我没有这样做(我是对的吗?).. 所以如果不清楚,这里是一个屏幕截图
有没有办法在日期部分 TktDate 上分组并忽略我的 ViewSource 的时间元素,或者我必须向我的类添加一个新属性以仅存储日期部分并在新属性上分组?
查看下面类的属性
public class Ticket : INotifyPropertyChanged
{
public int userid { get; set; }
public int deptid { get; set; }
public int topicid { get; set; }
public int staffid { get; set; }
public int priorityid { get; set; }
public string poster { get; set; }
public DateTime TktDate { get; set; }
public string title { get; set; }
public string bodyopen { get; set; }
public string bodyclose { get; set; }
public string timespent { get; set; }
public string dayoffset { get; set; }
public string sysdt { get; set; }
public string Netdt { get; set; }
public string Teldt { get; set; }
public string Clidt { get; set; }
public string status { get; set; }
private string ticket;
public string Tket
在 TktDate 上的 ViewSource I 组中,它记录了工单的创建日期。分组代码如下,效果很好
CollectionViewSource ticketViewSource = ((CollectionViewSource)(this.FindResource("ticketViewSource")));
ticketViewSource.Source = TicketCol;
ticketDataGrid.DataContext = ticketViewSource;
//add grouping
if (ticketViewSource != null)
{
ticketViewSource.GroupDescriptions.Clear();
ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate"));
ticketViewSource.SortDescriptions.Clear();
ticketViewSource.SortDescriptions.Add(new SortDescription("TktDate", ListSortDirection.Ascending));
}
【问题讨论】:
-
另一个属性是最简单的方法,否则你只是缺少一个自定义转换器,见this question。
标签: c# wpf wpfdatagrid