<!-- Copyright © Northwoods Software Corporation, 2008-2026. All Rights Reserved. -->

<UserControl x:Class="OrgChartBus.OrgChartBus"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:go="http://schemas.nwoods.com/GoXam"
    xmlns:local="clr-namespace:OrgChartBus">

  <UserControl.Resources>
    <go:BooleanBrushConverter x:Key="theBooleanBrushConverter"
                              FalseBrush="White" TrueBrush="Red" />

    <Style x:Key="AssistantTextStyle">
      <Setter Property="TextBlock.FontStyle" Value="Italic" />
    </Style>

    <go:DataTemplateDictionary x:Key="NodeTemplateDictionary">
      <!-- the Employee template -->
      <DataTemplate x:Key="">
        <Border BorderThickness="1" BorderBrush="Black"
              Padding="2,0,2,0" CornerRadius="3"
              Background="{Binding Path=Part.IsDropOntoAccepted,
                                   Converter={StaticResource theBooleanBrushConverter}}"
              go:Part.DropOntoBehavior="AddsLinkFromNode"
              go:Part.SelectionAdorned="True"
              MouseLeftButtonDown="Node_MouseLeftButtonDown"
              go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <StackPanel>
            <!-- the Name, Title, and Comments are two-way data bound to Employee properties
               and are editable in-place;
               MinWidth makes easier clicking when empty string -->
            <TextBlock Text="{Binding Path=Data.Name, Mode=TwoWay}" FontWeight="Bold"
                     go:Part.TextEditable="True" MinWidth="10" MinHeight="15" />
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Title: " />
              <TextBlock Text="{Binding Path=Data.Title, Mode=TwoWay}"
                       go:Part.TextEditable="True" MinWidth="10" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="ID: " />
              <TextBlock Text="{Binding Path=Data.Key}" />
              <TextBlock Text="  Boss: " />
              <TextBlock Text="{Binding Path=Data.ParentKey}" />
            </StackPanel>
            <!-- support wrapped text by specifying a MaxWidth;
               the MinHeight reserves space even when the Comment is an empty string -->
            <TextBlock Text="{Binding Path=Data.Comments, Mode=TwoWay}"
                     TextWrapping="Wrap" MaxWidth="200" MinWidth="10" MinHeight="15"
                     HorizontalAlignment="Left"
                     go:Part.TextEditable="True" go:Part.TextAspectRatio="2" />
          </StackPanel>
        </Border>
      </DataTemplate>

      <!-- the Assistant template -->
      <DataTemplate x:Key="Comment">
        <Border BorderThickness="1" BorderBrush="Black" Background="White"
              Padding="2,0,2,0" CornerRadius="3"
              go:Part.SelectionAdorned="True"
              go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <StackPanel Style="{StaticResource AssistantTextStyle}">
            <!-- the Name, Title, and Comments are two-way data bound to Employee properties
               and are editable in-place;
               MinWidth makes easier clicking when empty string -->
            <TextBlock Text="{Binding Path=Data.Name, Mode=TwoWay}" FontWeight="Bold"
                     go:Part.TextEditable="True" MinWidth="10" MinHeight="15" />
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Title: " />
              <TextBlock Text="{Binding Path=Data.Title, Mode=TwoWay}"
                       go:Part.TextEditable="True" MinWidth="10" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="ID: " />
              <TextBlock Text="{Binding Path=Data.Key}" />
              <TextBlock Text="  Boss: " />
              <TextBlock Text="{Binding Path=Data.ParentKey}" />
            </StackPanel>
            <!-- support wrapped text by specifying a MaxWidth;
               the MinHeight reserves space even when the Comment is an empty string -->
            <TextBlock Text="{Binding Path=Data.Comments, Mode=TwoWay}"
                     TextWrapping="Wrap" MaxWidth="200" MinWidth="10" MinHeight="15"
                     HorizontalAlignment="Left"
                     go:Part.TextEditable="True" go:Part.TextAspectRatio="2" />
          </StackPanel>
        </Border>
      </DataTemplate>
    </go:DataTemplateDictionary>

    <DataTemplate x:Key="LinkTemplate">
      <go:LinkShape Stroke="Black" StrokeThickness="1"
            go:Link.SelectionAdorned="True">
        <go:Link.Route>
          <go:Route Routing="Orthogonal" Corner="3" />
        </go:Link.Route>
      </go:LinkShape>
    </DataTemplate>
  </UserControl.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Padding="5"
               Text="Double click a node to add an employee, or shift-double click to add an assistant." />
    <go:Diagram x:Name="myDiagram" Grid.Row="1" Padding="5"
                HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                NodeTemplateDictionary="{StaticResource NodeTemplateDictionary}"
                LinkTemplate="{StaticResource LinkTemplate}">
      <go:Diagram.Layout>
        <!-- the tree generally grows downward;
             layout also happens automatically when a node grows or shrinks -->
        <!-- the child nodes of a "last parent" are laid out in a bus -->
        <go:TreeLayout Conditions="Standard NodeSizeChanged"
            TreeStyle="LastParents" Arrangement="Horizontal"
            Angle="90" LayerSpacing="35"
            AlternateAngle="90" AlternateAlignment="Bus"
            AlternateNodeSpacing="20" AlternateLayerSpacing="30" />
      </go:Diagram.Layout>
      <go:Diagram.DraggingTool>
        <go:DraggingTool DropOntoEnabled="True" />
      </go:Diagram.DraggingTool>
    </go:Diagram>
    <StackPanel  Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
      <Button Content="Save" Click="Save_Click" />
      <Button Content="Load" Click="Load_Click" x:Name="LoadButton" IsEnabled="False" />
    </StackPanel>
  </Grid>
</UserControl>
