Expanding and Collapsing C1OrgChart Nodes

C1OrgChart allows you to create a collapsible C1OrgChart that behaves similarly to a TreeView control. Complete the following steps to expand and collapse C1OrgChart nodes:

1.   From the Visual Studio File menu select New and choose Project.

2.   In the New Project dialog box choose a language in the left-side menu, choose .NET Framework 4 in the Framework drop-down list, and enter OrgChart as a name for the project.

3.   In the Solution Explorer, right-click the project name and choose Add Reference. In the Add Reference dialog box, locate and select the following assemblies and click OK to add references to your project:

      C1.Silverlight

      C1.Silverlight.OrgChart

4.   Add the xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" namespace to your namespace declarations in the <Window> tag. This is a more general namespace that will work with most ComponentOne Silverlight controls.

5.   Add the following namespace to your application's <Window> tag:

xmlns:local="clr-namespace:OrgChart"

6.   Insert the following XAML markup directly above the <c1:C1OrgChart></c1:C1OrgChart> tags to create the C1OrgChart data templates:

<Window.Resources>

        <!-- TemplateSelector: picks _tplDirector or _tlpOther -->

        <local:PersonTemplateSelector x:Key="_personTplSelector">

            <local:PersonTemplateSelector.DirectorTemplate>

                <!-- data template for Directors -->

                <DataTemplate>

                    <Border Background="Gold" BorderBrush="Black" BorderThickness="2 2 4 4" CornerRadius="6" Margin="20" MaxWidth="200">

                        <StackPanel Orientation="Vertical">

                            <Border CornerRadius="6 6 0 0" Background="Black">

                                <StackPanel Orientation="Horizontal">

                                    <CheckBox Margin="4 0" IsChecked="{Binding IsCollapsed, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=c1:C1OrgChart}}"/>

                                    <Ellipse Width="12" Height="12" Fill="Gold" Margin="4" />

                                    <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="16" Foreground="Gold" />

                                </StackPanel>

                            </Border>

                            <TextBlock Text="{Binding Position}" Padding="6 0" FontSize="14" FontStyle="Italic" HorizontalAlignment="Right" />

                        </StackPanel>

                    </Border>

                </DataTemplate>

            </local:PersonTemplateSelector.DirectorTemplate>

            <local:PersonTemplateSelector.OtherTemplate>

                <!-- data template for everyone else -->

                <DataTemplate>

                    <Border Background="WhiteSmoke" BorderBrush="Black" BorderThickness="1 1 2 2" CornerRadius="6" MaxWidth="200">

                        <StackPanel Orientation="Vertical">

                            <Border CornerRadius="6 6 0 0" Background="Black">

                                <StackPanel Orientation="Horizontal">

                                    <CheckBox Margin="4 0" IsChecked="{Binding IsCollapsed, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=c1:C1OrgChart}}"/>

                                    <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="14" Foreground="WhiteSmoke" Padding="4 0 0 0"></TextBlock>

                                </StackPanel>

                            </Border>

                            <TextBlock Text="{Binding Notes}" Padding="6 0" FontSize="9.5" TextWrapping="Wrap" />

                            <TextBlock Text="{Binding Position}" Padding="6 0" FontSize="12" FontStyle="Italic" HorizontalAlignment="Right" />

                        </StackPanel>

                    </Border>

                  </DataTemplate>

            </local:PersonTemplateSelector.OtherTemplate>

        </local:PersonTemplateSelector>

    </Window.Resources>

7.   Insert the following markup to create the C1OrgChart control and its control panel. The following XAML will add a ScrollViewer control in addition to the C1OrgChart control:

<!-- org chart -->

        <ScrollViewer Background="White" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Padding="0">

            <c1:C1OrgChart x:Name="_orgChart" Grid.Row="1" Orientation="Horizontal" ItemTemplateSelector="{StaticResource _personTplSelector}"

                           ConnectorStroke="Black" ConnectorThickness="2" IsCollapsed="False">

 

                <!-- scale transform bound to slider -->

                <c1:C1OrgChart.RenderTransform>

                    <ScaleTransform ScaleX="{Binding Value, ElementName=_sliderZoom}" ScaleY="{Binding Value, ElementName=_sliderZoom}" />

                </c1:C1OrgChart.RenderTransform>

 

                <!-- template used to show tree nodes -->

                <!-- not used in this sample since we are using a template selector -->

                <!--<c1:C1OrgChart.ItemTemplate />-->

            </c1:C1OrgChart>

        </ScrollViewer>

8.   Add the following XAML markup between the </Window.Resources> and the <c1:C1OrgChart> tags:

<!-- layout root -->

    <Grid x:Name="LayoutRoot">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition />

        </Grid.RowDefinitions>

 

        <!-- control panel -->

        <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 8">

            <Button Content="New Data" Padding="8 0" Click="Button_Click" />

            <TextBlock Text="  Zoom: " VerticalAlignment="Center" />

            <Slider x:Name="_sliderZoom" VerticalAlignment="Center" Minimum=".01" Maximum="1" Value="1" Width="200" />

        </StackPanel>

9.   Right-click the page and select View Code from the list. Import the following namespace into the code file:

      Visual Basic

      C#

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

      Visual Basic

      C#

11.  Add the following code to select the templates for items being created:

      Visual Basic

      C#

12.  Locate your application name in the Solution Explorer. Right-click on the name and select Add | New Item from the list. Select Code File from the template window and name the code file Person.cs or Person.vb.

13.  Add the following namespaces to the Person code file:

      Visual Basic

      C#

14.  Insert the following code below the namespaces to create the hierarchical data items which will be called to create the data in the C1OrgChart:

      Visual Basic

      C#

15.  Press F5 to run your application. The C1OrgChart should resemble the following image:

 

 

Click the CheckBox in the corner of one of the Main Item nodes. Note how the C1OrgChart collapses:

 

 


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.