Articles in this section
Category / Section

How to create Gantt Chart control in C# WPF?

2 mins read

Essential Gantt Chart for WPF is a MS Project-like Project Viewer with built-in grid, schedule and resource assignment constrains. The steps to create Gantt Chart control in C# WPF are provided below:

Step 1: Initialize the Gantt chart control.

Step 2: Create a collection of tasks in the view model and set the collection to the ItemsSource property of Gantt chart.

Step 3: Set the Gantt chart object to the content of the main window to display the Gantt chart.

The following code illustrates how to create Gantt Chart control in C# WPF

C#:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            GanttControl gantt = new GanttControl();
            ViewModel model = new ViewModel();
            gantt.ItemsSource = model.TaskCollection;
            this.Content = gantt;
        }
    }
 
public class ViewModel
    {
        public ObservableCollection<TaskDetails> TaskCollection { get; set; }
        public ViewModel()
        {
            TaskCollection = this.GetDataSource();
        }
 
        private ObservableCollection<TaskDetails> GetDataSource()
        {
            ObservableCollection<TaskDetails> task = new ObservableCollection<TaskDetails>();
            task.Add(
                new TaskDetails
                {
                    TaskId = 1,
                    TaskName = "Scope",
                    StartDate = new DateTime(2011, 1, 3),
                    FinishDate = new DateTime(2011, 1, 14),
                    Progress = 40d
                });
 
            task[0].Child.Add(
                new TaskDetails
                {
                    TaskId = 2,
                    TaskName = "Determine project office scope",
                    StartDate = new DateTime(2011, 1, 3),
                    FinishDate = new DateTime(2011, 1, 5),
                    Progress = 20d
                });
 
            task[0].Child.Add(
                new TaskDetails
                {
                    TaskId = 3,
                    TaskName = "Justify project office via business model",
                    StartDate = new DateTime(2011, 1, 6),
                    FinishDate = new DateTime(2011, 1, 7),
                    Duration = new TimeSpan(1, 0, 0, 0),
                    Progress = 20d
                });
 
            task[0].Child.Add(
                new TaskDetails
                {
                    TaskId = 4,
                    TaskName = "Secure executive sponsorship",
                    StartDate = new DateTime(2011, 1, 10),
                    FinishDate = new DateTime(2011, 1, 14),
                    Duration = new TimeSpan(1, 0, 0, 0),
                    Progress = 20d
                });
 
            task[0].Child.Add(
                new TaskDetails
                {
                    TaskId = 5,
                    TaskName = "Secure complete",
                    StartDate = new DateTime(2011, 1, 14),
                    FinishDate = new DateTime(2011, 1, 14),
                    Duration = new TimeSpan(1, 0, 0, 0),
                    Progress = 20d
                });
 
            return task;
        }
    }

Output:

Output Screenshot of Gantt Chart for WPF

You can find the sample in the link.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied