【问题标题】:Is it possible to add grouping on the index's of array column in an Observable Collection?是否可以在 Observablecollection 中的数组列的索引上添加分组?
【发布时间】:2019-08-02 17:14:18
【问题描述】:

我有一个带有预定义类的 ObservableCollection,目前 ObservableCollection 使用 ICollectionView 显示在 DataGrid 中,并按列 sl_Id、sl_Name、sl_Date 分组。

但是我想知道是否可以按 sl_struct 的索引进行分组,数组的长度是在运行时确定的。

public class SyncLog
{
    public string sl_ID { get; set; }
    public string sl_Name { get; set; }
    public string sl_Date { get; set; }
    public string sl_Type { get; set; }
    public string[] sl_Struct { get; set; }
    public string sl_SourceMachine { get; set; }
    public string sl_Source { get; set; }
    public string sl_DestMachine { get; set; }
    public string sl_Dest { get; set; }
    public bool sl_Success { get; set; }
    public string sl_Time { get; set; }
    public string sl_Size { get; set; }
}

当前分组代码

ICollectionView backupLogView = CollectionViewSource.GetDefaultView(Synclog);

PropertyGroupDescription group1 = new PropertyGroupDescription("sl_Id");
PropertyGroupDescription group2 = new PropertyGroupDescription("sl_Name");
PropertyGroupDescription group3 = new PropertyGroupDescription("sl_Date");

backupLogView.GroupDescriptions.Add(group1);
backupLogView.GroupDescriptions.Add(group2);
backupLogView.GroupDescriptions.Add(group3);

backupLogView.SortDescriptions.Add(new SortDescription("sl_Id", ListSortDirection.Ascending));
backupLogView.SortDescriptions.Add(new SortDescription("sl_Name", ListSortDirection.Ascending));
backupLogView.SortDescriptions.Add(new SortDescription("sl_Date", ListSortDirection.Ascending));
backupLogView.SortDescriptions.Add(new SortDescription("sl_Time", ListSortDirection.Ascending));

backupLogView.Refresh();

【问题讨论】:

    标签: c# wpf grouping observablecollection icollectionview


    【解决方案1】:

    如果new PropertyGroupDescription("sl_Struct.Length") 不起作用,虽然它可能应该起作用,但您可以在返回sl_Struct.Length 的 SyncLog 类中添加另一个属性

    public class SyncLog
    {
        public string[] sl_Struct { get; set; }
        public int sl_StructLength => sl_Struct?.Length ?? 0;
    }
    ...
    PropertyGroupDescription group = new PropertyGroupDescription("sl_StructLength ");
    

    如果您无法将属性添加到 SyncLog 类(例如,如果它是某个外部 DTO),那么您可能应该创建一个专门的 SyncLogViewModel 来包装常规 SyncLog 并添加 sl_StructLength

    public class SyncLogViewModel
    {
        private readonly SyncLog _syncLog;
        public SyncLogViewModel(SyncLog syncLog) =>
             _syncLog = syncLog ?? throw new ArgumentNullException(nameof(syncLog));
        public int sl_StructLength => _syncLog.sl_Struct?.Length ?? 0;
        public int sl_Struct 
        {
             get => _syncLog.sl_Struct;
             set => _syncLog.sl_Struct = value;
        }
        // Other properties...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多