DevExpress WPF使用技巧教程:WPF Gantt - Resources
下載DevExpress v20.1完整版 DevExpress v20.1漢化資源獲取
通過DevExpress WPF Controls,您能創(chuàng)建有著強(qiáng)大互動功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。
WPF Gantt控件(v20.1)允許您根據(jù)需要將資源分配給各個任務(wù),資源可以是成功完成實(shí)際任務(wù)所需的一切(從個人和員工到物理設(shè)備和資產(chǎn))。
資源綁定
使用 ResourcesSource 屬性將WPF Gantt控件綁定到資源集合。
<dxgn:GanttControl ItemsSource="{Binding Items}"> <dxgn:GanttControl.Columns> <dxgn:GanttColumn BindTo="Name"/> <dxgn:GanttColumn BindTo="StartDate"/> <dxgn:GanttColumn BindTo="FinishDate"/> </dxgn:GanttControl.Columns> <dxgn:GanttControl.View> <dxgn:GanttView ResourcesSource="{Binding Resources}" ... /> </dxgn:GanttControl.View> </dxgn:GanttControl> public class StartupBusinessPlanViewModel { public List<GanttTask> Items { get; private set; } public List<GanttResource> Resources { get; private set; } // ... public StartupBusinessPlanViewModel() { this.Items = CreateData(); this.Resources = CreateResources(); // ... } List<GanttTask> CreateData() { var tasks = new List<GanttTask>(); // ... tasks.Add(new GanttTask { Id = 53, ParentId = 48, Name = "Describe strengths, weaknesses, assets and threats", StartDate = new DateTime(2019, 1, 9, 13, 0, 0), FinishDate = new DateTime(2019, 1, 10, 12, 0, 0), }); tasks.Add(new GanttTask { Id = 54, ParentId = 48, Name = "Estimate sales volume during startup period", StartDate = new DateTime(2019, 1, 10, 13, 0, 0), FinishDate = new DateTime(2019, 1, 11, 12, 0, 0), }); // ... return tasks; } List<GanttResource> CreateResources() { var resources = new List<GanttResource>(); resources.Add(new GanttResource { Name = "Business Advisor", Id = 1 }); resources.Add(new GanttResource { Name = "Peers", Id = 2 }); resources.Add(new GanttResource { Name = "Lawyer", Id = 3 }); resources.Add(new GanttResource { Name = "Government Agency", Id = 4 }); resources.Add(new GanttResource { Name = "Manager", Id = 5 }); resources.Add(new GanttResource { Name = "Owners", Id = 6 }); resources.Add(new GanttResource { Name = "Accountant", Id = 7 }); resources.Add(new GanttResource { Name = "Banker", Id = 8 }); resources.Add(new GanttResource { Name = "Information Services", Id = 9 }); return resources; } }
上面的代碼示例使用MVVM庫中提供的內(nèi)置數(shù)據(jù)類,但是您也可以將WPF Gantt控件鏈接到您的自定義資源類。
檢索資源依賴項(xiàng)
將WPF甘特圖控件綁定到資源后,將這些資源分配給任務(wù)。 您的數(shù)據(jù)源可以通過以下方式存儲資源依賴性:
- 在一個單獨(dú)的集合中,其中每個元素都包含一個任務(wù) - 資源對。
- 在包含有關(guān)其資源信息的任務(wù)對象中。
檢索存儲在單獨(dú)集合中的資源依賴項(xiàng)
收集項(xiàng)目應(yīng)包含任務(wù)和資源字段。
public class GanttResourceLink { public object TaskId { get; set; } public object ResourceId { get; set; } }
將ResourceLinksSource屬性綁定到資源依賴項(xiàng)的集合(由下面的代碼示例中的ResourceLinks ViewModel屬性公開)。
<dxgn:GanttControl ItemsSource="{Binding Tasks}"> <dxgn:GanttControl.View> <dxgn:GanttView ... ResourcesSource="{Binding Resources}" ResourceLinksSource="{Binding ResourceLinks}"> </dxgn:GanttView> </dxgn:GanttControl.View> </dxgn:GanttControl>
檢索存儲在任務(wù)對象中的資源依賴關(guān)系
收集項(xiàng)目應(yīng)包含一個資源字段。
public class GanttResourceLink { public object ResourceId { get; set; } }
任務(wù)對象應(yīng)提供對任務(wù)資源集合的訪問。
public class GanttTask { public object Id { get; set; } public object ParentId { get; set; } public string Name { get; set; } public DateTime? StartDate { get; set; } public DateTime? FinishDate { get; set; } public ObservableCollection<GanttResourceLink> ResourceLinks { get; set; } // ... }
將資源依賴關(guān)系數(shù)據(jù)字段的路徑分配給GanttView.ResourceLinksPath屬性(由下面的代碼示例中的ResourceLinks任務(wù)屬性公開)。
<dxgn:GanttControl ItemsSource="{Binding Tasks}"> <dxgn:GanttControl.View> <dxgn:GanttView ... ResourcesSource="{Binding Resources}" ResourceLinksPath="ResourceLinks"> </dxgn:GanttView> </dxgn:GanttControl.View> </dxgn:GanttControl>
DevExpress 5月線上公開課報(bào)名火熱開啟,教你入門報(bào)表(Report)控件
DevExpress技術(shù)交流群:775869749 歡迎一起進(jìn)群討論
掃描關(guān)注DevExpress中文網(wǎng)微信公眾號,及時獲取最新動態(tài)及最新資訊