【问题标题】:Using part of the Date in a ViewSource Grouping Description WPF C#在 ViewSource 分组描述 WPF C# 中使用部分日期
【发布时间】: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


【解决方案1】:

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

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

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

如果这不起作用,您应该向您的 Ticket 类添加一个新的 DateTime 只读属性,该类返回 TktDate.Date 并按此分组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多