Version

Code Example Creating View for xamGantt using Custom Classes

Topic Overview

Purpose

You can create custom classes for project columns, project tables and project views and use those classes to customize the appearance of your xamGantt .

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic describes how you can add the xamGantt control to a page.

Code Example: Creating Customized View for xamGantt

Description

The code example shows you how to create custom project column classes, custom project table classes and custom project view classes. Then you can create a ListBackedProjectViewProvider in XAML and set its ColumnItemsSource, TableItemsSource and ViewItemsSource to the corresponding collections of those custom objects. The mapping between custom classes is made with ColumnPropertyMappings, TablePropertyMappings and ViewPropertyMappings properties of the ListBackedProjectViewProvider.

Finally, the ViewProvider property of the xamGantt is set to the ListBackedProjectViewProvider instance.

This code example includes a collection of sample custom tasks.

Prerequisites

To complete the code example, you should have a xamGantt project. You can follow the instructions in Adding xamGantt to a Page in order to create a sample xamGantt project.

Then you need to modify the project. The following classes should be included in your project:

You have to replace the mark-up and the ProjectViewModel class with those ones shown in the code section.

Preview

This is a preview of the completed sample project. The screen-shot is taken with the ViewKey="View01". This instance of the CustomProjectView class with Id="View01" sets the xamGantt to show critical tasks as well as the summary task. The column set is specified by the ColumnIds property of the CustomProjectTable class instance, used by the custom view.

xamGantt Code Example Creating View for xamGantt using custom classes 1.png

Code

In XAML:

<Grid>
        <Grid.Resources>
            <local:ProjectViewModel x:Key="dc" />
        </Grid.Resources>
        <Grid.DataContext>
            <Binding Source="{StaticResource dc}" />
        </Grid.DataContext>
        <!-- ListBackedProjectViewProvider Setup-->
        <ig:ListBackedProjectViewProvider x:Name="lbpViewProvider" ColumnItemsSource="{Binding Columns}"
                                          TableItemsSource="{Binding Tables}" ViewItemsSource="{Binding CustomViews}">
            <ig:ListBackedProjectViewProvider.ColumnPropertyMappings>
                <ig:ProjectColumnPropertyMappingCollection UseDefaultMappings="True">
                    <ig:ProjectColumnPropertyMapping ColumnProperty="Id" DataObjectProperty="Id" />
                </ig:ProjectColumnPropertyMappingCollection>
            </ig:ListBackedProjectViewProvider.ColumnPropertyMappings>
            <ig:ListBackedProjectViewProvider.TablePropertyMappings>
                <ig:ProjectTablePropertyMappingCollection UseDefaultMappings="True" />
            </ig:ListBackedProjectViewProvider.TablePropertyMappings>
            <ig:ListBackedProjectViewProvider.ViewPropertyMappings>
                <ig:ProjectViewPropertyMappingCollection UseDefaultMappings="True" />
            </ig:ListBackedProjectViewProvider.ViewPropertyMappings>
        </ig:ListBackedProjectViewProvider>
        <ig:XamGantt x:Name="xamGantt" Grid.Row="0" Project="{Binding SampleProject}"
                     ViewProvider="{Binding ElementName=lbpViewProvider}" ViewKey="View01" />
</Grid>

In C#:

public class ProjectViewModel :
INotifyPropertyChanged
    {
        #region Private variables
        private Project project;
        private ObservableCollection<CustomProjectView> views;
        private ObservableCollection<CustomProjectTable> tables;
        private ObservableCollection<CustomProjectColumn> columns;
        private CustomProjectView selectedView;
        #endregion // Private variables
        #region Public Properties
        public Project SampleProject
        {
            get
            {
                if (project == null)
                {
                    project = ProjectDataHelper.GenerateProjectData();
                }
                return project;
            }
            set
            {
                if (project != value)
                {
                    project = value;
                    OnPropertyChanged("SampleProject");
                }
            }
        }
        public CustomProjectView SelectedView
        {
            get
            {
                return selectedView;
            }
            set
            {
                if (value != selectedView)
                {
                    selectedView = value;
                    OnPropertyChanged("SelectedView");
                }
            }
        }
        public ObservableCollection<CustomProjectColumn> Columns
        {
            get
            {
                if (columns == null)
                {
                    columns = GenerateColumns();
                }
                return columns;
            }
        }
        public ObservableCollection<CustomProjectTable> Tables
        {
            get
            {
                if (tables == null)
                {
                    tables = GenerateTables();
                }
                return tables;
            }
            set
            {
                if (tables != value)
                {
                    tables = value;
                }
            }
        }
        public ObservableCollection<CustomProjectView> CustomViews
        {
            get
            {
                if (views == null)
                {
                    views = GenerateViews();
                }
                return views;
            }
            set
            {
                if (views != value)
                {
                    views = value;
                    OnPropertyChanged("CustomViews");
                }
            }
        }
        #endregion // Public properties
        #region Private helpers
        private ObservableCollection<CustomProjectColumn> GenerateColumns()
        {
            return new ObservableCollection<CustomProjectColumn>()
            {
                new CustomProjectColumn
                {
                    Id = "idTaskName",
                    Key = "TaskName",
                    HeaderText = "Name"
                },
                new CustomProjectColumn
                {
                    Id = "idStart",
                    Key = "ManualStart",
                    HeaderText = "Start",
                    HeaderTextHorizontalAlignment = "Center"
                },
                new CustomProjectColumn
                {
                    Id = "idFinish",
                    Key = "ManualFinish",
                    HeaderText = "Finish",
                    HeaderTextHorizontalAlignment = "Center"
                },
                new CustomProjectColumn
                {
                    Id = "idDuration",
                    Key = "ManualDuration",
                    HeaderText = "Duration"
                },
                new CustomProjectColumn
                {
                    Id = "idPredecessors",
                    Key = "PredecessorsIdText",
                    HeaderText = "Predecessors"
                },
                new CustomProjectColumn
                {
                    Id="idIsCritical",
                    Key = "IsCritical",
                    HeaderText = "Is Critical"
                }
            };
        }
        private ObservableCollection<CustomProjectTable> GenerateTables()
        {
            return new ObservableCollection<CustomProjectTable>()
            {
                new CustomProjectTable
                {
                    Key = "Table01",
                    // Comma separated column ids, which are provided by the ProjectColumnProperty.DataItemId
property mapping
                    ColumnIds = "idTaskName, idStart, idDuration, idFinish, idPredecessors,
idIsCritical",
                    ShowInMenu = true
                },
                new CustomProjectTable
                {
                    Key = "Table02",
                    ColumnIds = "idTaskName, idStart, idFinish, idIsCritical",
                    ShowInMenu = true
                },
                new CustomProjectTable
                {
                    Key = "Table03",
                    ColumnIds = "idTaskName, idDuration, idPredecessors",
                    ShowInMenu = true
                }
            };
        }
        private ObservableCollection<CustomProjectView> GenerateViews()
        {
            return new ObservableCollection<CustomProjectView>()
            {
                new CustomProjectView
                {
                    Key = "View01",
                    AreSummaryTasksVisible = true,
                    AreCriticalTasksHighlighted = true,
                    // Comma separated column keys, where each key can optionally be followed by 'Ascending' or
'Descending' word to indicate
                    // that the column should be sorted ascending or descending
                    SortedColumns = "ManualStart, ManualFinish:Descending",
                    IsOutlineStructurePreservedWhenSorting = true,
                    TableKey = "Table01",
                    NonWorkingTimeHighlightStyle = "ActualNonWorkingHours"
                },
                new CustomProjectView
                {
                    Key = "View02",
                    TableKey = "Table02",
                    AreCriticalTasksHighlighted = false,
                    AreSummaryTasksVisible = false
                },
                new CustomProjectView
                {
                    Key = "View03",
                    TableKey = "Table03",
                    AreCriticalTasksHighlighted = true
                }
            };
        }
        #endregion // Private helpers
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string
propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs
(propertyName));
            }
        }
        #endregion // INotifyPropertyChanged
    }

In Visual Basic:

Public Class ProjectViewModel
      Implements INotifyPropertyChanged
      #Region "Private variables"
      Private project As Project
      Private views As ObservableCollection(Of CustomProjectView)
      Private m_tables As ObservableCollection(Of CustomProjectTable)
      Private m_columns As ObservableCollection(Of CustomProjectColumn)
      Private m_selectedView As CustomProjectView
      #End Region
      #Region "Public Properties"
      Public Property SampleProject() As
Project
            Get
                  If project Is Nothing Then
                        project = ProjectDataHelper.GenerateProjectData()
                  End If
                  Return project
            End Get
            Set
                  If project <> value Then
                        project = value
                        OnPropertyChanged("SampleProject")
                  End If
            End Set
      End Property
      Public Property SelectedView() As
CustomProjectView
            Get
                  Return m_selectedView
            End Get
            Set
                  If value <> m_selectedView Then
                        m_selectedView = value
                        OnPropertyChanged("SelectedView")
                  End If
            End Set
      End Property
      Public ReadOnly Property Columns() As ObservableCollection(Of CustomProjectColumn)
            Get
                  If m_columns Is Nothing
Then
                        m_columns = GenerateColumns()
                  End If
                  Return m_columns
            End Get
      End Property
      Public Property Tables() As
ObservableCollection(Of CustomProjectTable)
            Get
                  If m_tables Is Nothing Then
                        m_tables = GenerateTables()
                  End If
                  Return m_tables
            End Get
            Set
                  If m_tables <> value Then
                        m_tables = value
                  End If
            End Set
      End Property
      Public Property CustomViews() As
ObservableCollection(Of CustomProjectView)
            Get
                  If views Is Nothing Then
                        views = GenerateViews()
                  End If
                  Return views
            End Get
            Set
                  If views <> value Then
                        views = value
                        OnPropertyChanged("CustomViews")
                  End If
            End Set
      End Property
      #End Region
      #Region "Private helpers"
      Private Function GenerateColumns() As
ObservableCollection(Of CustomProjectColumn)
            Return New ObservableCollection(Of CustomProjectColumn)() From
{ _
                  New CustomProjectColumn() With { _
                        Key .Id = "idTaskName", _
                        Key .Key = "TaskName", _
                        Key .HeaderText = "Name" _
                  }, _
                  New CustomProjectColumn() With { _
                        Key .Id = "idStart", _
                        Key .Key = "ManualStart", _
                        Key .HeaderText = "Start", _
                        Key .HeaderTextHorizontalAlignment = "Center" _
                  }, _
                  New CustomProjectColumn() With { _
                        Key .Id = "idFinish", _
                        Key .Key = "ManualFinish", _
                        Key .HeaderText = "Finish", _
                        Key .HeaderTextHorizontalAlignment = "Center" _
                  }, _
                  New CustomProjectColumn() With { _
                        Key .Id = "idDuration", _
                        Key .Key = "ManualDuration", _
                        Key .HeaderText = "Duration" _
                  }, _
                  New CustomProjectColumn() With { _
                        Key .Id = "idPredecessors", _
                        Key .Key = "PredecessorsIdText", _
                        Key .HeaderText = "Predecessors" _
                  }, _
                  New CustomProjectColumn() With { _
                        Key .Id = "idIsCritical", _
                        Key .Key = "IsCritical", _
                        Key .HeaderText = "Is Critical" _
                  } _
            }
      End Function
      Private Function GenerateTables() As
ObservableCollection(Of CustomProjectTable)
                        ' Comma separated column ids, which are provided by the
ProjectColumnProperty.DataItemId property mapping
            Return New ObservableCollection(Of CustomProjectTable)() From {
_
                  New CustomProjectTable() With { _
                        Key .Key = "Table01", _
                        Key .ColumnIds = "idTaskName, idStart, idDuration, idFinish, idPredecessors,
idIsCritical", _
                        Key .ShowInMenu = True _
                  }, _
                  New CustomProjectTable() With { _
                        Key .Key = "Table02", _
                        Key .ColumnIds = "idTaskName, idStart, idFinish, idIsCritical", _
                        Key .ShowInMenu = True _
                  }, _
                  New CustomProjectTable() With { _
                        Key .Key = "Table03", _
                        Key .ColumnIds = "idTaskName, idDuration, idPredecessors", _
                        Key .ShowInMenu = True _
                  } _
            }
      End Function
      Private Function GenerateViews() As
ObservableCollection(Of CustomProjectView)
                        ' Comma separated column keys, where each key can optionally be followed by 'Ascending'
or 'Descending' word to indicate
                        ' that the column should be sorted ascending or descending
            Return New ObservableCollection(Of CustomProjectView)() From {
_
                  New CustomProjectView() With { _
                        Key .Key = "View01", _
                        Key .AreSummaryTasksVisible = True, _
                        Key .AreCriticalTasksHighlighted = True, _
                        Key .SortedColumns = "ManualStart, ManualFinish:Descending", _
                        Key .IsOutlineStructurePreservedWhenSorting = True, _
                        Key .TableKey = "Table01", _
                        Key .NonWorkingTimeHighlightStyle = "ActualNonWorkingHours" _
                  }, _
                  New CustomProjectView() With { _
                        Key .Key = "View02", _
                        Key .TableKey = "Table02", _
                        Key .AreCriticalTasksHighlighted = False, _
                        Key .AreSummaryTasksVisible = False _
                  }, _
                  New CustomProjectView() With { _
                        Key .Key = "View03", _
                        Key .TableKey = "Table03", _
                        Key .AreCriticalTasksHighlighted = True _
                  } _
            }
      End Function
      #End Region
      #Region "INotifyPropertyChanged"
      Public Event PropertyChanged As
PropertyChangedEventHandler
      Public Sub OnPropertyChanged(propertyName As String)
            RaiseEvent PropertyChanged(Me, New
PropertyChangedEventArgs(propertyName))
      End Sub
      #End Region
End Class

The following topics provide additional information related to this topic:

Topic Purpose

The topics in this group contain information about the xamGantt ListBackedProject ViewProvider.

This topic gives an overview of the main features of the xamGantt Calendars. xamGantt makes time calculations using calendars.

You can create customize the xamGantt view by using ListBackedProject ViewProvider and built-in project column, project table and project view classes.