ComponentOne Scheduler for WPF and Silverlight
Scheduler for WPF Tutorials / Creating a Custom Grouping View / Step 2 of 4: Adding Code to the Application
In This Topic
    Step 2 of 4: Adding Code to the Application
    In This Topic

    In this step you will add code to the application to handle adding contacts and to control returning the VisualIntervalCollection for a specified day.

    1. Right-click on MainPage.xaml and select View Code from the list.
    2. Add the following namespaces to your application:
      Visual Basic
      Copy Code
      Imports C1.C1Schedule
      Imports C1.WPF.Schedule
      Imports C1.WPF
      Imports System.Collections
      

       

      C#
      Copy Code
      using C1.C1Schedule;
      using C1.WPF.Schedule;
      using C1.WPF;
      using System.Collections;
      

       

    3. Insert the following code directly below the InitializeComponent() method:

      Visual Basic
      Copy Code
      ' add some contacts
         Dim cnt As New Contact()
         cnt.Text = "Andy Garcia"
         c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt)
         cnt = New Contact()
         cnt.Text = "Nancy Drew"
         c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt)
         cnt = New Contact()
         cnt.Text = "Robert Clark"
         c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt)
         cnt = New Contact()
         cnt.Text = "James Doe"
         c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt)
         c1Scheduler1.GroupBy = "Contact"
      

       

      C#
      Copy Code
      // add some contacts
          Contact cnt = new Contact();
          cnt.Text = "Andy Garcia";
          c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt);
          cnt = new Contact();
          cnt.Text = "Nancy Drew";
          c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt);
          cnt = new Contact();
          cnt.Text = "Robert Clark";
          c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt);
          cnt = new Contact();
          cnt.Text = "James Doe";
          c1Scheduler1.DataStorage.ContactStorage.Contacts.Add(cnt);
          c1Scheduler1.GroupBy = "Contact";
      

    4. The following code returns the VisualIntervalCollection for the specified day and SchedulerGroup:

      Visual Basic
      Copy Code
      ''' <summary>
         ''' Returns VisualIntervalCollection for the specified day and specified SchedulerGroup which can be used
         ''' as an ItemsSource for VisualIntervalsPresenter control.
         ''' </summary>
         ''' <remarks>
         ''' If converter parameter is "Self", return list of a single VisualIntervalGroup, to use it as ItemsSource for representing all-day area.
         ''' In all other cases returns VisualItervalCollection containing time slots for the single day.
         ''' </remarks>
         Public Class GroupItemToVisualIntervalsConverter
               Implements IValueConverter
               Public Shared [Default] As New GroupItemToVisualIntervalsConverter()
               #Region "IValueConverter Members"
               Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
                     Dim el As FrameworkElement = TryCast(value, FrameworkElement)
                     If el IsNot Nothing Then
                           Dim group As SchedulerGroupItem = TryCast(el.DataContext, SchedulerGroupItem)
                           Dim index As Integer = -1
                           If group IsNot Nothing Then
                                 Dim itm As ItemsControl = TryCast(VTreeHelper.GetParentOfType(el, GetType(ItemsControl)), ItemsControl)
                                 If itm IsNot Nothing Then
                                       Dim data As Object = itm.DataContext
                                       Dim itmParent As ItemsControl = TryCast(VTreeHelper.GetParentOfType(itm, GetType(ItemsControl)), ItemsControl)
                                       If itmParent IsNot Nothing Then
                                             index = itmParent.Items.IndexOf(data)
                                             Dim visualIntervalGroup As VisualIntervalGroup = TryCast(group.VisualIntervalGroups(index), VisualIntervalGroup)
                                             Dim param As String = CStr(parameter)
                                             If param.ToLower() = "self" Then
                                                   ' create list of a single VisualIntervalGroup
                                                   ' (we need list to use it as ItemsSource)
                                                   Dim list As New List(Of Object)()
                                                   list.Add(visualIntervalGroup)
                                                   Return list
                                             Else
                                                   Return visualIntervalGroup.VisualIntervals
                                             End If
                                       End If
                                 End If
                           End If
                     End If
                     Return Nothing
               End Function
               Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
                     Throw New NotImplementedException()
               End Function
               #End Region
         End Class
      End Namespace
      

       

      C#
      Copy Code
      /// <summary>
          /// Returns VisualIntervalCollection for the specified day and specified SchedulerGroup which can be used
          /// as an ItemsSource for VisualIntervalsPresenter control.
          /// </summary>
          /// <remarks>
          /// If converter parameter is "Self", return list of a single VisualIntervalGroup, to use it as ItemsSource for representing all-day area.
          /// In all other cases returns VisualItervalCollection containing time slots for the single day.
          /// </remarks>
          public class GroupItemToVisualIntervalsConverter : IValueConverter
          {
              public static GroupItemToVisualIntervalsConverter Default = new GroupItemToVisualIntervalsConverter();
              #region IValueConverter Members
              public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
              {
                  FrameworkElement el = value as FrameworkElement;
                  if (el != null)
                  {
                      SchedulerGroupItem group = el.DataContext as SchedulerGroupItem;
                      int index = -1;
                      if (group != null)
                      {
                          ItemsControl itm = VTreeHelper.GetParentOfType(el, typeof(ItemsControl)) as ItemsControl;
                          if (itm != null)
                          {
                              object data = itm.DataContext;
                              ItemsControl itmParent = VTreeHelper.GetParentOfType(itm, typeof(ItemsControl)) as ItemsControl;
                              if (itmParent != null)
                              {
                                  index = itmParent.Items.IndexOf(data);
                                  VisualIntervalGroup visualIntervalGroup = group.VisualIntervalGroups[index] as VisualIntervalGroup;
                                  string param = (string)parameter;
                                  if (param.ToLower() == "self")
                                  {
                                      // create list of a single VisualIntervalGroup
                                      // (we need list to use it as ItemsSource)
                                      List<object> list = new List<object>();
                                      list.Add(visualIntervalGroup);
                                      return list;
                                  }
                                  else
                                  {
                                      return visualIntervalGroup.VisualIntervals;
                                  }
                              }
                          }
                      }
                  }
                  return null;
              }
              public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
              {
                  throw new NotImplementedException();
              }
      
              #endregion
          }
      }
      

    In this step you added code to create contacts and to return  VisualIntervalCollections for the specified day. In the next step you will create your Resource Dictionary.