OrgChart for WPF and Silverlight | ComponentOne
Quick Start : OrgChart for WPF and Silverlight / Step 2 of 3: Adding Content to the C1OrgChart Control
In This Topic
    Step 2 of 3: Adding Content to the C1OrgChart Control
    In This Topic

    In the previous step you created a WPF or Silverlight application and added the C1OrgChart control to your project. In this step you'll add content to the C1OrgChart control. To customize your project and add content to the C1OrgChart control in your application, complete the following steps:

    1. In the Solution Explorer, right-click the MainWindow.xaml and MainPage.xaml file and select View Code. The code file will open.
    2. Edit the code file so that it appears similar to the following:

      Visual Basic
      Copy Code
      Partial Public Class MainWindow
          Inherits Window
          Public Sub New()
              InitializeComponent()
              CreateData()
          End Sub
          Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
              CreateData()
          End Sub
          Private Sub CheckBox_Click(sender As Object, e As RoutedEventArgs)
              _orgChart.Orientation = If(DirectCast(sender, CheckBox).IsChecked.Value, Orientation.Horizontal, Orientation.Vertical)
          End Sub
          Private Sub CreateData()
              Dim p = Data.Person.CreatePerson(10)
              _tbTotal.Text = String.Format(" ({0} items total)", p.TotalCount)
              _orgChart.Header = p
          End Sub
      End Class
      Public Class PersonTemplateSelector
          Inherits DataTemplateSelector
          Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
              Dim p = TryCast(item, Data.Person)
              Dim e = TryCast(container, FrameworkElement)
              Return If(p.Position.IndexOf("Director") > -1, TryCast(e.Resources("_tplDirector"), DataTemplate), TryCast(e.Resources("_tplOther"), DataTemplate))
          End Function
      End Class
      

       

      C#
      Copy Code
      namespace QuickStart
      {
          public partial class MainWindow : Window
          {
              public MainWindow()
              {
                  InitializeComponent();
                  CreateData();
              }
              void Button_Click(object sender, RoutedEventArgs e)
              {
                  CreateData();
              }
              void CheckBox_Click(object sender, RoutedEventArgs e)
              {
                  _orgChart.Orientation = ((CheckBox)sender).IsChecked.Value
                      ? Orientation.Horizontal
                      : Orientation.Vertical;
              }
              void CreateData()
              {
                  var p = Data.Person.CreatePerson(10);
                  _tbTotal.Text = string.Format(" ({0} items total)", p.TotalCount);
                  _orgChart.Header = p;
              }
          }
          public class PersonTemplateSelector : DataTemplateSelector
          {
              public override DataTemplate SelectTemplate(object item, DependencyObject container)
              {
                  var p = item as Data.Person;
                  var e = container as FrameworkElement;
                  return p.Position.IndexOf("Director") > -1
                      ? e.Resources["_tplDirector"] as DataTemplate
                      : e.Resources["_tplOther"] as DataTemplate;
              }
          }
      }
      
    3. Navigate to the Solution Explorer, right-click the project name, and select Add │ New Item. In the Add New Item dialog box, locate the Code File template, name it "EnumerationExtension", and click Add.

      The new code file will open. Note that this code file is also included with the sample, if you choose you can simply add that file.

    4. Add the following code to the EnumerationExtension code file :

      Visual Basic
      Copy Code
      Imports System.Windows
      Imports System.Windows.Controls
      Imports System.Windows.Markup
      Imports System.Reflection
      Imports System.Collections.Generic
      Namespace Util
          Public Class EnumerationExtension
              Inherits MarkupExtension
              Public Property EnumType() As Type
                  Get
                      Return m_EnumType
                  End Get
                  Set(value As Type)
                      m_EnumType = Value
                  End Set
              End Property
              Private m_EnumType As Type
              Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
                  Dim list = New List(Of Object)()
                  For Each value In EnumType.GetFields(BindingFlags.[Public] Or BindingFlags.[Static])
                      list.Add(value.GetValue(Nothing))
                  Next
                  Return list
              End Function
          End Class
      End Namespace
      

       

      C#
      Copy Code
      using System;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Markup;
      using System.Reflection;
      using System.Collections.Generic;
      namespace Util
      {
          public class EnumerationExtension : MarkupExtension
          {
              public Type EnumType { get; set; }
              public override object ProvideValue(IServiceProvider serviceProvider)
              {
                  var list = new List<object>();
                  foreach (var value in EnumType.GetFields(BindingFlags.Public | BindingFlags.Static))
                  {
                      list.Add(value.GetValue(null));
                  }
                  return list;
              }
          }
      }
      

      This code allows you to bind the ComboBox controls to enumeration values so at run time you can choose a bound value to set.

    5. Navigate to the Solution Explorer, right-click the project name, and select Add │ New Item. In the Add New Item dialog box, locate the Code File template, name it "Person", and click Add.

      The new code file will open. Note that this code file is also included with the sample, if you choose you can simply add that file.

    6. Add the following code to the Person code file:

      Visual Basic
      Copy Code
      Imports System.Collections
      Imports System.Collections.Generic
      Imports System.Collections.ObjectModel
      Namespace Data
          Public Class Person
              Private _list As New ObservableCollection(Of Person)()
      #Region "** object model"
              Public Property Name() As String
                  Get
                      Return m_Name
                  End Get
                  Set(value As String)
                      m_Name = Value
                  End Set
              End Property
              Private m_Name As String
              Public Property Position() As String
                  Get
                      Return m_Position
                  End Get
                  Set(value As String)
                      m_Position = Value
                  End Set
              End Property
              Private m_Position As String
              Public Property Notes() As String
                  Get
                      Return m_Notes
                  End Get
                  Set(value As String)
                      m_Notes = Value
                  End Set
              End Property
              Private m_Notes As String
              Public ReadOnly Property Subordinates() As IList(Of Person)
                  Get
                      Return _list
                  End Get
              End Property
              Public ReadOnly Property TotalCount() As Integer
                  Get
                      Dim count = 1
                      For Each p In Subordinates
                          count += p.TotalCount
                      Next
                      Return count
                  End Get
              End Property
              Public Overrides Function ToString() As String
                  Return String.Format("{0}:" & vbCr & vbLf & vbTab & "{1}", Name, Position)
              End Function
      #End Region
      #Region "** Person factory"
              Shared _rnd As New Random()
              Shared _positions As String() = "Director|Manager|Designer|Developer|Writer|Assistant".Split("|"c)
              Shared _areas As String() = "Development|Marketing|Sales|Support|Accounting".Split("|"c)
              Shared _first As String() = "John|Paul|Dan|Dave|Rich|Mark|Greg|Erin|Susan|Sarah|Tim|Trevor|Kevin|Mark|Dewey|Huey|Larry|Moe|Curly|Adam|Albert".Split("|"c)
              Shared _last As String() = "Smith|Doe|Williams|Sorensen|Hansen|Mandela|Johnson|Ward|Woodman|Jordan|Mays|Kevorkian|Trudeau|Hendrix|Clinton".Split("|"c)
              Shared _verb As String() = "likes|reads|studies|hates|exercises|dreams|plays|writes|argues|sleeps|ignores".Split("|"c)
              Shared _adjective As String() = "long|short|important|pompous|hard|complex|advanced|modern|boring|strange|curious|obsolete|bizarre".Split("|"c)
              Shared _noun As String() = "products|tasks|goals|campaigns|books|computers|people|meetings|food|jokes|accomplishments|screens|pages".Split("|"c)
              Public Shared Function CreatePerson(level As Integer) As Person
                  Dim p = CreatePerson()
                  If level > 0 Then
                      level -= 1
                      For i As Integer = 0 To _rnd.[Next](1, 4) - 1
                          p.Subordinates.Add(CreatePerson(_rnd.[Next](level \ 2, level)))
                      Next
                  End If
                  Return p
              End Function
              Public Shared Function CreatePerson() As Person
                  Dim p = New Person()
                  p.Position = String.Format("{0} of {1}", GetItem(_positions), GetItem(_areas))
                  p.Name = String.Format("{0} {1}", GetItem(_first), GetItem(_last))
                  p.Notes = String.Format("{0} {1} {2} {3}", p.Name, GetItem(_verb), GetItem(_adjective), GetItem(_noun))
                  While _rnd.NextDouble() < 0.5
                      p.Notes += String.Format(" and {0} {1} {2}", GetItem(_verb), GetItem(_adjective), GetItem(_noun))
                  End While
                  p.Notes += "."
                  Return p
              End Function
              Private Shared Function GetItem(list As String()) As String
                  Return list(_rnd.[Next](0, list.Length))
              End Function
      #End Region
          End Class
      End Namespace
      

       

      C#
      Copy Code
      using System;
      using System.Collections;
      using System.Collections.Generic;
      using System.Collections.ObjectModel;
      namespace Data
      {
          public class Person
          {
              ObservableCollection<Person> _list = new ObservableCollection<Person>();
              #region ** object model
              public string Name { get; set; }
              public string Position { get; set; }
              public string Notes { get; set; }
              public IList<Person> Subordinates
              {
                  get { return _list; }
              }
              public int TotalCount
              {
                  get
                  {
                      var count = 1;
                      foreach (var p in Subordinates)
                      {
                          count += p.TotalCount;
                      }
                      return count;
                  }
              }
              public override string ToString()
              {
                  return string.Format("{0}:\r\n\t{1}", Name, Position);
              }
              #endregion
              #region ** Person factory
              static Random _rnd = new Random();
              static string[] _positions = "Director|Manager|Designer|Developer|Writer|Assistant".Split('|');
              static string[] _areas = "Development|Marketing|Sales|Support|Accounting".Split('|');
              static string[] _first = "John|Paul|Dan|Dave|Rich|Mark|Greg|Erin|Susan|Sarah|Tim|Trevor|Kevin|Mark|Dewey|Huey|Larry|Moe|Curly|Adam|Albert".Split('|');
              static string[] _last = "Smith|Doe|Williams|Sorensen|Hansen|Mandela|Johnson|Ward|Woodman|Jordan|Mays|Kevorkian|Trudeau|Hendrix|Clinton".Split('|');
              static string[] _verb = "likes|reads|studies|hates|exercises|dreams|plays|writes|argues|sleeps|ignores".Split('|');
              static string[] _adjective = "long|short|important|pompous|hard|complex|advanced|modern|boring|strange|curious|obsolete|bizarre".Split('|');
              static string[] _noun = "products|tasks|goals|campaigns|books|computers|people|meetings|food|jokes|accomplishments|screens|pages".Split('|');
              public static Person CreatePerson(int level)
              {
                  var p = CreatePerson();
                  if (level > 0)
                  {
                      level--;
                      for (int i = 0; i < _rnd.Next(1, 4); i++)
                      {
                          p.Subordinates.Add(CreatePerson(_rnd.Next(level / 2, level)));
                      }
                  }
                  return p;
              }
              public static Person CreatePerson()
              {
                  var p = new Person();
                  p.Position = string.Format("{0} of {1}", GetItem(_positions), GetItem(_areas));
                  p.Name = string.Format("{0} {1}", GetItem(_first), GetItem(_last));
                  p.Notes = string.Format("{0} {1} {2} {3}", p.Name, GetItem(_verb), GetItem(_adjective), GetItem(_noun));
                  while (_rnd.NextDouble() < .5)
                  {
                      p.Notes += string.Format(" and {0} {1} {2}", GetItem(_verb), GetItem(_adjective), GetItem(_noun));
                  }
                  p.Notes += ".";
                  return p;
              }
              static string GetItem(string[] list)
              {
                  return list[_rnd.Next(0, list.Length)];
              }
              #endregion
          }
      }
      

      This code creates a simple class with a recursive ObservableCollection for items to be displayed in C1OrgChart and creates a method to build a simple organization chart. Note that the C1OrgChart control's default template uses the ToString method to dictate what text is displayed.  You will need to override this method (as in the above code) if you're not using a DataTemplate (if you are using a DataTemplate, you can just set your bindings in that and ignore the ToString).

    In this step you added content to the C1OrgChart control. In the next step you'll view some of the run-time interactions possible in the control.