C# 按隐藏属性对datagrid条目进行分组
我有以下数据结构:C# 按隐藏属性对datagrid条目进行分组,c#,wpf,datagrid,grouping,C#,Wpf,Datagrid,Grouping,我有以下数据结构: public class TimeData { public Employee Employee { get; set; } public IList<WorkTime> WorkTimes { get; set; } } 我已经尝试了以下代码: ListCollectionView collectionView = new ListCollectionView(this.viewModel.TimeDatas); collectionView.
public class TimeData
{
public Employee Employee { get; set; }
public IList<WorkTime> WorkTimes { get; set; }
}
我已经尝试了以下代码:
ListCollectionView collectionView = new ListCollectionView(this.viewModel.TimeDatas);
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Employee"));
this.grdTimeData.ItemsSource = collectionView;
此选项按员工分组,但不显示工作时间列表:
在网格行中,我只需要工作时间,员工仅用于分组。您需要将数据转换为
DataGrid
可以处理的格式。创建包含所有特性的视图模型类:
public class EmployeeAndWorkTime
{
public string Name { get; set; }
public DateTime Date { get; set; }
public double Hours { get; set; }
public string TimeCode { get; set; }
}
…并绑定到从现有TimeDatas
集合创建的IEnumerable
:
TransformedTimeDatas = TimeDatas.Select(timeData =>
{
EmployeeAndWorkTime[] viewModels = new EmployeeAndWorkTime[timeData.WorkTimes.Count];
for (int i = 0; i < timeData.WorkTimes.Count; ++i)
viewModels[i] = new EmployeeAndWorkTime()
{
Name = string.Format("{0} {1}", timeData.Employee.FirstName, timeData.Employee.LastName),
Date = timeData.WorkTimes[i].Date,
Hours = timeData.WorkTimes[i].Hours,
TimeCode = timeData.WorkTimes[i].TimeCode
};
return viewModels;
}).ToArray();
ListCollectionView collectionView = new ListCollectionView(TransformedTimeDatas);
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
this.grdTimeData.ItemsSource = collectionView;
TransformedTimeDatas=TimeDatas。选择(timeData=>
{
EmployeeAndWorkTime[]视图模型=新EmployeeAndWorkTime[timeData.WorkTimes.Count];
对于(int i=0;i
public class EmployeeAndWorkTime
{
public string Name { get; set; }
public DateTime Date { get; set; }
public double Hours { get; set; }
public string TimeCode { get; set; }
}
TransformedTimeDatas = TimeDatas.Select(timeData =>
{
EmployeeAndWorkTime[] viewModels = new EmployeeAndWorkTime[timeData.WorkTimes.Count];
for (int i = 0; i < timeData.WorkTimes.Count; ++i)
viewModels[i] = new EmployeeAndWorkTime()
{
Name = string.Format("{0} {1}", timeData.Employee.FirstName, timeData.Employee.LastName),
Date = timeData.WorkTimes[i].Date,
Hours = timeData.WorkTimes[i].Hours,
TimeCode = timeData.WorkTimes[i].TimeCode
};
return viewModels;
}).ToArray();
ListCollectionView collectionView = new ListCollectionView(TransformedTimeDatas);
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
this.grdTimeData.ItemsSource = collectionView;