C1OrgChart では、TreeView コントロールと同様に動作する折りたたみ可能な C1OrgChart を作成できます。C1OrgChart ノードを展開/折りたたむには、次の手順に従います。
Visual Studio の[ファイル]メニューから、[新規作成]を選択し、[プロジェクト]を選択します。
[新しいプロジェクト]ダイアログボックスで、左側のメニューから言語を選択します。[フレームワーク]ドロップダウンリストで[。NET Framework 4]を選択し、プロジェクトの名前として「OrgChart」と入力します。
ソリューションエクスプローラで、プロジェクト名を右クリックし、[参照の追加]を選択します。[参照の追加]ダイアログボックスで、以下のアセンブリを見つけて選択し、[OK]をクリックしてプロジェクトに参照を追加します。
C1.WPF および C1.WPF.OrgChart
C1.Silverlight および C1.Silverlight.OrgChart
xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" 名前空間を <Window> タグの名前空間宣言に追加します。これは、ほとんどの WPF コントロールで機能する一般的な名前空間です。
次の名前空間をアプリケーションの <Window> タグに追加します。
xmlns:local="clr-namespace:OrgChart"
次の XAML マークアップを <c1:C1OrgChart> </c1:C1OrgChart> タグの直前に挿入して、C1OrgChart データテンプレートを作成します。
| XAML |
コードのコピー
|
|---|---|
<Window.Resources>
<!--テンプレートセレクタ:_tplDirectorまたは _tlpOther を選択します -->
<local:PersonTemplateSelector x:Key="_personTplSelector">
<local:PersonTemplateSelector.DirectorTemplate>
<!--役員用のデータテンプレート -->
<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>
<!--その他の従業員用のデータテンプレート -->
<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>
|
|
次のマークアップを挿入して、C1OrgChart コントロールとそのコントロールパネルを作成します。次の XAML は、C1OrgChart コントロールに加えて、ScrollViewer コントロールを追加します。
| XAML |
コードのコピー
|
|---|---|
<!--組織図 -->
<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">
<!--スケール変換をスライダに連結する -->
<c1:C1OrgChart.RenderTransform>
<ScaleTransform ScaleX="{Binding Value, ElementName=_sliderZoom}"
ScaleY="{Binding Value, ElementName=_sliderZoom}" />
</c1:C1OrgChart.RenderTransform>
<!--ツリーノードの表示に使用されるテンプレート -->
<!--テンプレートセレクタを使用するため、このサンプルでは使用しません -->
<!--<c1:C1OrgChart.ItemTemplate />-->
</c1:C1OrgChart>
</ScrollViewer>
|
|
次の XAML マークアップを </Window.Resources> タグと <c1:C1OrgChart> タグの間に追加します。
| XAML |
コードのコピー
|
|---|---|
<!--レイアウトルート -->
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!--コントロールパネル -->
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 8">
<Button Content="新しいデータ" Padding="8 0" Click="Button_Click" />
<TextBlock Text=" ズーム: " VerticalAlignment="Center" />
<Slider x:Name="_sliderZoom" VerticalAlignment="Center" Minimum=".01" Maximum="1" Value="1" Width="200" />
</StackPanel>
|
|
ページを右クリックし、リストから[コードの表示]を選択します。次の名前空間をコードファイルにインポートします。
InitializeComponent() メソッドのすぐ下に、次のコードを挿入します。
次のコードを追加して、作成される項目のテンプレートを選択します。
ソリューションエクスプローラで、アプリケーション名を見つけます。名前を右クリックし、リストから[追加]→[新しい項目]を選択します。テンプレートウィンドウで[コードファイル]を選択し、コードファイル名を「Person。cs」または「Person。vb」と指定します。
次の名前空間を Person コードファイルに追加します。
名前空間の下に次のコードを挿入して、C1OrgChart のデータを作成するために呼び出される階層化データ項目を作成します。
[F5]キーを押してアプリケーションを実行します。C1OrgChart は次の図のようになります。

メイン項目ノードの隅にあるチェックボックスをクリックします。C1OrgChart が折りたたまれることを確認します。
