是否可以在 Observable 集合中的数组列的索引上添加分组?
Is it possible to add grouping on the index's of array column in an Observable Collection?
我有一个带有预定义 class 的 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();
如果 new PropertyGroupDescription("sl_Struct.Length")
不起作用,虽然它可能应该起作用,但您可以将另一个 属性 添加到您的 SyncLog class 即 returns sl_Struct.Length
public class SyncLog
{
public string[] sl_Struct { get; set; }
public int sl_StructLength => sl_Struct?.Length ?? 0;
}
...
PropertyGroupDescription group = new PropertyGroupDescription("sl_StructLength ");
如果您不能将 属性 添加到 SyncLog
class(例如,如果它是一些外部 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...
}
我有一个带有预定义 class 的 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();
如果 new PropertyGroupDescription("sl_Struct.Length")
不起作用,虽然它可能应该起作用,但您可以将另一个 属性 添加到您的 SyncLog class 即 returns sl_Struct.Length
public class SyncLog
{
public string[] sl_Struct { get; set; }
public int sl_StructLength => sl_Struct?.Length ?? 0;
}
...
PropertyGroupDescription group = new PropertyGroupDescription("sl_StructLength ");
如果您不能将 属性 添加到 SyncLog
class(例如,如果它是一些外部 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...
}