亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按隱藏屬性對數據網格條目進行分組

按隱藏屬性對數據網格條目進行分組

C#
楊__羊羊 2022-10-23 16:28:52
我有以下數據結構:public class TimeData{    public Employee Employee { get; set; }    public IList<WorkTime> WorkTimes { get; set; }}視圖模型:public ObservableCollection<TimeData> TimeDatas { get; set; }在我的數據網格中,我想顯示所有工作時間。按員工分組。像這樣:日期 | 小時 | 時間碼喬恩·多伊19 年 12 月 3 日 | 8 | 43319 年 3 月 13 日 | 8 | 43319 年 3 月 14 日 | 5 | 546邁克·穆斯特19 年 12 月 3 日 | 4 | 65319 年 3 月 13 日 | 3 | 43319 年 3 月 14 日 | 9 | 546public class Employee{    public string FirstName { get; set; }    public string LastName { get; set; }}public class WorkTime{    public DateTime Date { get; set; }    public double Hours { get; set; }    public string TimeCode { get; set; }}我已經嘗試過以下代碼:ListCollectionView collectionView = new ListCollectionView(this.viewModel.TimeDatas);collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Employee"));this.grdTimeData.ItemsSource = collectionView;這按員工分組,但不顯示 WorkTimes 列表:在網格行中,我只需要 WorkTimes,Employee 僅用于分組。
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

您需要將數據轉換為DataGrid可以處理的格式。創建一個包含所有屬性的視圖模型類:


public class EmployeeAndWorkTime

{

    public string Name { get; set; }

    public DateTime Date { get; set; }

    public double Hours { get; set; }

    public string TimeCode { get; set; }

}

...并綁定到IEnumerable<EmployeeAndWorkTime>您從現有TimeDatas集合創建的:


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;


查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號