wpf-400349-controls-and-libraries-gantt-control-task-dependencies.md
The GanttControl can show relationships between tasks. These relationships are called task dependencies. They indicate dependency criteria based on which tasks begin and end in relation to each other. For example, the FinishToStart dependency means that the second task may not start before the first task is finished.
The following task dependency types are available:
Note
To specify task dependencies, you must specify the GanttView.KeyFieldName property value.
In your data source, the task dependencies can be stored in any of the following ways:
The GanttControl supports both of them.
If you store task dependencies in a separate collection, the collection items should contain the following fields:
The code sample below demonstrates the data model that represents task dependencies.
public class Link {
// Specifies the predecessor task's Id
public int PredecessorId { get; set; }
// Specifies the successor task's Id
public int SuccessorId { get; set; }
// Specifies the task dependency type (FinishToStart, FinishToFinish, etc.)
public PredecessorLinkType Type { get; set; }
// Specifies the time lag between two tasks
public System.TimeSpan TimeLag { get; set; }
}
To retrieve task dependencies from a data source, do the following:
The code sample below illustrates a GanttControl bound to a data source and to a collection of task dependencies.
<dxgn:GanttControl ItemsSource="{Binding Tasks}">
<dxgn:GanttControl.Columns>
<dxgn:GanttColumn BindTo="Name"/>
<dxgn:GanttColumn BindTo="StartDate"/>
<dxgn:GanttColumn BindTo="FinishDate"/>
</dxgn:GanttControl.Columns>
<dxgn:GanttControl.View>
<dxgn:GanttView
AutoExpandAllNodes="True"
KeyFieldName="Id"
ChildNodesPath="Children"
StartDateMapping="Start"
FinishDateMapping="Finish"
NameMapping="Name"
TreeDerivationMode="ChildNodesSelector"
PredecessorLinksSource="{Binding Dependencies}">
<dxgn:GanttView.PredecessorLinkMappings>
<dxgn:GanttPredecessorLinkMappings
PredecessorTask="PredecessorId"
Task="SuccessorId"
LinkType="Type"
Lag="TimeLag"/>
</dxgn:GanttView.PredecessorLinkMappings>
</dxgn:GanttView>
</dxgn:GanttControl.View>
</dxgn:GanttControl>
If you store task’s dependencies in the task object, the collection items should contain the following fields:
The code sample below demonstrates the data model that represents task dependencies.
public class Link {
// Specifies the predecessor task's Id
public int PredecessorId { get; set; }
// Specifies the task dependency type (FinishToStart, FinishToFinish, etc.)
public PredecessorLinkType Type { get; set; }
}
If you store task’s dependencies in the task object, the task object should provide access to a collection of task’s dependencies, like in the code sample below:
public class Task {
public int Id { get; set; }
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime Finish { get; set; }
public ObservableCollection<Task> Children { get; } = new ObservableCollection<Task>();
public ObservableCollection<Link> DependencyLinks { get; } = new ObservableCollection<Link>();
}
To retrieve task dependencies from task objects, do the following:
The code sample below illustrates a GanttControl bound to a data source and to a collection of task dependencies.
<dxgn:GanttControl ItemsSource="{Binding Tasks}">
<dxgn:GanttControl.Columns>
<dxgn:GanttColumn BindTo="Name"/>
<dxgn:GanttColumn BindTo="StartDate"/>
<dxgn:GanttColumn BindTo="FinishDate"/>
</dxgn:GanttControl.Columns>
<dxgn:GanttControl.View>
<dxgn:GanttView
AutoExpandAllNodes="True"
ChildNodesPath="Children"
StartDateMapping="Start"
FinishDateMapping="Finish"
NameMapping="Name"
TreeDerivationMode="ChildNodesSelector"
PredecessorLinksPath="DependencyLinks">
<dxgn:GanttView.PredecessorLinkMappings>
<dxgn:GanttPredecessorLinkMappings
PredecessorTask="Task"
LinkType="Type"/>
</dxgn:GanttView.PredecessorLinkMappings>
</dxgn:GanttView>
</dxgn:GanttControl.View>
</dxgn:GanttControl>
You can simplify your ViewModel if your tasks use only the FinishToStart dependency type.
In this case, your task objects should provide access to a collection of task predecessors (or their Id’s), like in the code sample below.
public class Task {
public int Id { get; set; }
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime Finish { get; set; }
public ObservableCollection<Task> Children { get; } = new ObservableCollection<Task>();
// Each tasks have a collection of its predecessor tasks (identified by Id's)
public ObservableCollection<int> PredecessorIds { get; } = new ObservableCollection<int>();
}
To retrieve task dependencies from task objects, do the following:
Provide a path to a task dependency data field to the GanttView.PredecessorLinksPath property (exposed by the PredecessorIds task property in the code sample below).
Specify the mappings to dependency objects using the GanttView.PredecessorLinkMappings property like in the code sample below.
<dxgn:GanttControl ItemsSource="{Binding Tasks}">
...
<dxgn:GanttControl.View>
<dxgn:GanttView
AutoExpandAllNodes="True"
KeyFieldName="Id"
ChildNodesPath="Children"
StartDateMapping="Start"
FinishDateMapping="Finish"
NameMapping="Name"
PredecessorLinksPath="PredecessorIds"
TreeDerivationMode="ChildNodesSelector">
<dxgn:GanttView.PredecessorLinkMappings>
<dxgn:GanttPredecessorLinkMappings PredecessorTask="." />
</dxgn:GanttView.PredecessorLinkMappings>
</dxgn:GanttView>
</dxgn:GanttControl.View>
</dxgn:GanttControl>