在ViewSource分组中使用Date的一部分描述WPF C#

问题描述 投票:0回答:1

我有一个基于C#类的一组对象的可观察集合的ViewSource,这个对象称为“Ticket”,用于C#.net 4中的WPF项目的DataGrid。该项目用于一个简单的Helpdesk应用程序。

ViewSource在Property调用TktDate上分组,当天输入的所有票证显然在我的DataGrid中组合在一起

但是我最近更新了代码,所以TktDate现在不仅存储了Date元素,还存储了Time元素,所以很明显,当我指的是日期上的Group时,由于时间元素,我没有做到这一点(我是否正确? )..所以如果不清楚这里是一个屏幕截图enter image description here

有没有办法在日期部分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));
            }
c# wpf wpfdatagrid
1个回答
0
投票

有没有办法在日期部分TktDate上分组并忽略我的ViewSource的时间元素,或者我必须在我的类中添加一个新属性来存储日期部分并在新属性上分组?

您可以尝试按嵌套属性进行分组:

ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate.Date"));

如果这不起作用,你应该添加一个新的DateTime只读属性到你的Ticket类,它返回TktDate.Date和group by this。

© www.soinside.com 2019 - 2024. All rights reserved.