当前位置:   article > 正文

wpf devexpress 添加GanttControl到项目_devexpress ganttcontrol

devexpress ganttcontrol

这个教程示范如何添加GanttControl 到你的项目使用内置GanttControl数据类。

要求

添加 Devexpress.Wpf.Gantt Nuget包到你的项目使用GanttControl.

数据模型

GanttControl携带和内置数据对象,可以使用创建视图模型:

GanttTask

呈现甘特图任务

GanttPredecessorLink

呈现任务关系

GanttTask类曝光如下属性:

属性描述
Id指定任务id
ParentId指定任务父id
StartDate指定任务开始日期
FinishDate指定任务结束日期
Progress指定任务进程
Name指定任务名称和标题
BaselineStartDate指定任务基线开始日期
BaselineFinishDate指定任务基线完成日期
PredecessorLinks提供访问任务记录集合

Id和ParentId属性允许组织任务等级体系在空白数据集合

GanttPredecessorLink提供如下属性

属性描述
PredecessorTask Id指定访问记录Id
LinkType指定任务关系类型(完成ToStart,FinishToFinish,等等)
Lag指定依赖时间lag

添加视图模型

创建视图模型类暴露Tasks属性ObservableCollection<GanttTask>类型

代码例子如下示范了视图模型

  1. using DevExpress.Mvvm.Gantt;
  2. using System;
  3. using System.Collections.ObjectModel;
  4. namespace GanttControlDemoApp {
  5. public class ProjectTaskViewModel {
  6. public ObservableCollection<GanttTask> Tasks { get; set; }
  7. public ProjectTaskViewModel() {
  8. Tasks = new ObservableCollection<GanttTask> {
  9. new GanttTask() {
  10. Id = 0,
  11. Name = "Add a new feature",
  12. StartDate = DateTime.Now.AddDays(-1),
  13. FinishDate = DateTime.Now.AddDays(6)
  14. },
  15. new GanttTask() {
  16. Id =1,
  17. ParentId = 0,
  18. Name = "Write the code",
  19. StartDate = DateTime.Now.AddDays(-1),
  20. FinishDate = DateTime.Now.AddDays(2)
  21. },
  22. new GanttTask() {
  23. Id = 2,
  24. ParentId = 0,
  25. Name = "Write the docs",
  26. StartDate = DateTime.Now.AddDays(2),
  27. FinishDate = DateTime.Now.AddDays(5)
  28. },
  29. new GanttTask() {
  30. Id = 3,
  31. ParentId = 0,
  32. Name = "Test the new feature",
  33. StartDate = DateTime.Now.AddDays(2),
  34. FinishDate = DateTime.Now.AddDays(5)
  35. },
  36. new GanttTask() {
  37. Id = 4,
  38. ParentId = 0,
  39. Name = "Release the new feature",
  40. StartDate = DateTime.Now.AddDays(5),
  41. FinishDate = DateTime.Now.AddDays(6),
  42. }
  43. };
  44. }
  45. }
  46. }

添加Gantt Control到视图

添加GanttControl到项目

打开vs工具箱,找到DX.18.2:Data & Analytics 页面,拖动GanttControl 工具箱内容,拖动到window控件

传递视图模型到视图DataContext属性,绑定GanttControl的ItemsSource属性到视图模型Task属性

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:GanttControlDemoApp"
  5. xmlns:dxgn="http://schemas.devexpress.com/winfx/2008/xaml/gantt"
  6. x:Class="GanttControlDemoApp.MainWindow">
  7. <Window.DataContext>
  8. <local:ProjectTaskViewModel />
  9. </Window.DataContext>
  10. <Grid>
  11. <dxgn:GanttControl ItemsSource="{Binding Tasks}" />
  12. </Grid>
  13. </Window>

图片如下演示了结果

GanttControl显示统计任务和折叠子任务。显示数据行和任务属性和显示所有任务

添加任务行

使用控件的GanttControl.Columns属性添加行

甘特图行显示通过GanttColumn类。绑定行到任何任务标准属性使用BindTo 属性。像如下代码

  1. <dxgn:GanttControl ItemsSource="{Binding Tasks}">
  2. <dxgn:GanttControl.Columns>
  3. <dxgn:GanttColumn BindTo="Name" />
  4. <dxgn:GanttColumn BindTo="StartDate" />
  5. <dxgn:GanttColumn BindTo="FinishDate" />
  6. </dxgn:GanttControl.Columns>
  7. </dxgn:GanttControl>

设置GanttView

GanttView指定甘特图表内容和显示

扩展所有甘特图任务当控件加载。设置AutoExpandAllNodes属性为true。可以显示和编辑和排序内容被设置视图AllowEditing和AllowSorting属性为false,像下面的代码例子

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:GanttControlDemoApp"
  5. xmlns:dxgn="http://schemas.devexpress.com/winfx/2008/xaml/gantt"
  6. x:Class="GanttControlDemoApp.MainWindow">
  7. <Window.DataContext>
  8. <local:ProjectTaskViewModel />
  9. </Window.DataContext>
  10. <Grid>
  11. <dxgn:GanttControl ItemsSource="{Binding Tasks}">
  12. <dxgn:GanttControl.Columns>
  13. <dxgn:GanttColumn BindTo="Name" />
  14. <dxgn:GanttColumn BindTo="StartDate" />
  15. <dxgn:GanttColumn BindTo="FinishDate" />
  16. </dxgn:GanttControl.Columns>
  17. <dxgn:GanttControl.View>
  18. <dxgn:GanttView AutoExpandAllNodes="True" AllowEditing="False" AllowSorting="False"/>
  19. </dxgn:GanttControl.View>
  20. </dxgn:GanttControl>
  21. </Grid>
  22. </Window>

下面的图片演示了结果

任务依赖

每一个任务暴露了PredecessorLinks属性。属性提供了访问GanttPredecessorLink集合对象。每一个GanttPredecessorLink对象包含任务访问记录id和相对的链接类型

添加如下代码到视图模型

  1. // the "Release the new feature" task can begin only when the "Write the docs" task is complete
  2. Tasks[4].PredecessorLinks.Add(new GanttPredecessorLink() { PredecessorTaskId = 2, LinkType = PredecessorLinkType.FinishToStart });
  3. // the "Release the new feature" task can begin only when the "Test the new feature" task is complete
  4. Tasks[4].PredecessorLinks.Add(new GanttPredecessorLink() { PredecessorTaskId = 3, LinkType = PredecessorLinkType.FinishToStart });
  5. // the "Write the docs" task can begin only when the "Write the code" task is complete
  6. Tasks[2].PredecessorLinks.Add(new GanttPredecessorLink() { PredecessorTaskId = 1, LinkType = PredecessorLinkType.FinishToStart });
  7. // the "Test the new feature" task can begin only when the "Write the code" task is complete
  8. Tasks[3].PredecessorLinks.Add(new GanttPredecessorLink() { PredecessorTaskId = 1, LinkType = PredecessorLinkType.FinishToStart });

现在,GanttControl显示任务关系。如下图片显示结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/230469
推荐阅读
相关标签
  

闽ICP备14008679号