<UserControl x:Class="AIStudio.Wpf.DiagramDesigner.DiagramControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dd="clr-namespace:AIStudio.Wpf.DiagramDesigner"
             xmlns:c="clr-namespace:AIStudio.Wpf.DiagramDesigner.Controls"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 
             mc:Ignorable="d" HorizontalAlignment="Center" VerticalAlignment="Center"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/AIStudio.Wpf.DiagramDesigner;component/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
            <dd:ColorBrushConverter x:Key="ColorBrushConverter" />         
            <dd:InvertBoolConverter x:Key="InvertBoolConverter"/>
            <dd:NotNullOrEmptyToBoolConverter x:Key="NotNullOrEmptyToBoolConverter"/>

            <!-- ResizeDecorator Default Template -->
            <!--
            <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
                <Grid SnapsToDevicePixels="true">
                    <c:ResizeThumb Height="1" Cursor="SizeNS" Margin="0 0 0 0"
                     VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
                    <c:ResizeThumb Width="1" Cursor="SizeWE" Margin="0 0 0 0"
                     VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
                    <c:ResizeThumb Width="1" Cursor="SizeWE" Margin="0 0 0 0"
                     VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
                    <c:ResizeThumb Height="1" Cursor="SizeNS" Margin="0 0 0 0"
                     VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
                    <c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-3 -3 0 0"
                     VerticalAlignment="Top" HorizontalAlignment="Left"/>
                    <c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -3 -3 0"
                     VerticalAlignment="Top" HorizontalAlignment="Right"/>
                    <c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-3 0 0 -3"
                     VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
                    <c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -3 -3"
                     VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
                </Grid>
            </ControlTemplate>-->

            <Style TargetType="{x:Type Shape}" x:Key="ThumbCorner">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="Stroke" Value="#FFC8C8C8" />
                <Setter Property="StrokeThickness" Value=".5" />
                <Setter Property="Width" Value="7" />
                <Setter Property="Height" Value="7" />
                <Setter Property="Margin" Value="-2" />
                <Setter Property="Fill">
                    <Setter.Value>
                        <RadialGradientBrush Center="0.2, 0.2" GradientOrigin="0.2, 0.2" RadiusX="0.8" RadiusY="0.8">
                            <GradientStop Color="White" Offset="0.0" />
                            <GradientStop Color="Gray" Offset="0.8" />
                        </RadialGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--ResizeDecorator Default Template-->
            <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
                <Grid>
                    <Grid Opacity="0" Margin="-3">
                        <c:RotateThumb x:Name="rotate" Width="7"
                             Height="7"
                             Margin="0,-20,0,0"
                             Cursor="Hand"
                             VerticalAlignment="Top"
                             HorizontalAlignment="Center"/>
                        <c:ResizeThumb Height="3"
                             Cursor="SizeNS"
                             VerticalAlignment="Top"
                             HorizontalAlignment="Stretch"/>
                        <c:ResizeThumb Width="3"
                             Cursor="SizeWE"
                             VerticalAlignment="Stretch"
                             HorizontalAlignment="Left"/>
                        <c:ResizeThumb Width="3"
                             Cursor="SizeWE"
                             VerticalAlignment="Stretch"
                             HorizontalAlignment="Right"/>
                        <c:ResizeThumb Height="3"
                             Cursor="SizeNS"
                             VerticalAlignment="Bottom"
                             HorizontalAlignment="Stretch"/>
                        <c:ResizeThumb Width="7"
                             Height="7"
                             Margin="-2"
                             Cursor="SizeNWSE"
                             VerticalAlignment="Top"
                             HorizontalAlignment="Left"/>
                        <c:ResizeThumb Width="7"
                             Height="7"
                             Margin="-2"
                             Cursor="SizeNESW"
                             VerticalAlignment="Top"
                             HorizontalAlignment="Right"/>
                        <c:ResizeThumb Width="7"
                             Height="7"
                             Margin="-2"
                             Cursor="SizeNESW"
                             VerticalAlignment="Bottom"
                             HorizontalAlignment="Left"/>
                        <c:ResizeThumb Width="7"
                             Height="7"
                             Margin="-2"
                             Cursor="SizeNWSE"
                             VerticalAlignment="Bottom"
                             HorizontalAlignment="Right"/>
                    </Grid>
                    <Grid IsHitTestVisible="False" Opacity="1" Margin="-3">
                        <Rectangle SnapsToDevicePixels="True"
                         StrokeThickness="1"
                         Margin="1"
                         Stroke="Gray"/>
                        <Line x:Name="line" StrokeThickness="1" X1="0" Y1="0" X2="0" Y2="20"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Top"
                    Margin="0,-19,0,0"
                    Stroke="Gray"/>
                        <Ellipse x:Name="top" Style="{StaticResource ThumbCorner}" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="-1,-20,0,0"/>
                        <Ellipse Style="{StaticResource ThumbCorner}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                        <Ellipse Style="{StaticResource ThumbCorner}" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                        <Ellipse Style="{StaticResource ThumbCorner}" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
                        <Ellipse Style="{StaticResource ThumbCorner}" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
                    </Grid>
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding ShowRotate}" Value="false">
                        <Setter TargetName="rotate" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="line" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="top" Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

            <ObjectDataProvider x:Key="ConnectorOrientationMenu" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="dd:ConnectorOrientation" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>

            <Style x:Key="CinchMenuItemStyle" TargetType="{x:Type MenuItem}" >
                <Setter Property="Header" Value="{Binding Text}" />
                <Setter Property="ItemsSource" Value="{Binding Children}" />
                <Setter Property="Command" Value="{Binding Command}" />
                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
            </Style>

            <!-- Connector Style -->
            <Style x:Key="PointConnectorStyle" TargetType="{x:Type dd:PointConnector}">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type dd:PointConnector}">
                            <Grid Width="{Binding ConnectorWidth}" Height="{Binding ConnectorHeight}">
                                <!-- transparent extra space makes connector easier to hit -->
                                <Ellipse Fill="Transparent" Margin="-3" />
                                <Ellipse Fill="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}"  
                                         StrokeThickness="1" 
                                         Stroke="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ConnectorVertexType}" Value="Start">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type dd:PointConnector}">
                                    <Grid Width="{Binding ConnectorWidth}" 
                                          Height="{Binding ConnectorHeight}"
                                          UseLayoutRounding="True"
                                          SnapsToDevicePixels="True">
                                        <Ellipse Stroke="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}"
                                              StrokeThickness="1"
                                              Margin="-2.5"/>
                                        <Rectangle 
                                              Fill="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}"
                                              Margin="1"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ConnectorVertexType}" Value="End">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type dd:PointConnector}">
                                    <Grid Width="{Binding ConnectorWidth}" 
                                          Height="{Binding ConnectorHeight}"
                                          UseLayoutRounding="True"
                                          SnapsToDevicePixels="True">
                                        <Ellipse Stroke="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}"
                                              StrokeThickness="1"
                                              Margin="-2.5"/>
                                        <Ellipse 
                                              Fill="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}"
                                              Margin="1"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <Style x:Key="GifImageConnectorContainer" TargetType="{x:Type dd:ConnectorContainer}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ItemsControl}">
                            <Grid>
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                                <Rectangle Fill="#7F243859" Opacity="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="{Binding ShouldInsertAnchor, Converter={StaticResource BooleanToVisibilityConverter}}">
                                    <i:Interaction.Behaviors>
                                        <dd:ControlMouseLeftButtonDownCommandBehavior Command="{Binding AddItemCommand}" />
                                    </i:Interaction.Behaviors>
                                </Rectangle>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="ItemsControlStyle" TargetType="{x:Type ItemsControl}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ItemsControl}">
                            <Grid>
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <ControlTemplate x:Key="ConnectorDecoratorTemplate"
                     TargetType="{x:Type Control}">
                <Grid Margin="{Binding ConnectorMargin}">
                    <dd:Connector 
                        Content="{Binding LeftConnector}" 
                        SnapsToDevicePixels="True"
                        Orientation="Left"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding TopLeftConnector}" 
                        SnapsToDevicePixels="True"
                        Orientation="TopLeft"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding TopConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="Top"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding TopRightConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="TopRight"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding RightConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="Right"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding BottomRightConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="BottomRight"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding BottomConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="Bottom"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <dd:Connector 
                        Content="{Binding BottomLeftConnector}"
                        SnapsToDevicePixels="True"
                        Orientation="BottomLeft"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" />
                    <!--<Button Margin="-30"  Background="Transparent"    BorderThickness="0"                
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" >
                        <TextBlock Text="✚" FontWeight="Bold" FontSize="16" Foreground="DarkGray" />
                    </Button>
                    <Button Margin="-30"  Background="Transparent"    BorderThickness="0"                
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" >
                        <TextBlock Text="✚" FontWeight="Bold" FontSize="16" Foreground="DarkGray" />
                    </Button>
                    <Button Margin="-30"  Background="Transparent"    BorderThickness="0"                
                        VerticalAlignment="Top"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" >
                        <TextBlock Text="✚" FontWeight="Bold" FontSize="16" Foreground="DarkGray" />
                    </Button>
                    <Button Margin="-30"  Background="Transparent"    BorderThickness="0"                
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}" >
                        <TextBlock Text="✚" FontWeight="Bold" FontSize="16" Foreground="DarkGray" />
                    </Button>-->                    
                </Grid>
            </ControlTemplate>

            <ControlTemplate x:Key="InnerConnectorDecoratorTemplate"
                     TargetType="{x:Type Control}">
                <dd:ConnectorContainer x:Name="PART_ConnectorContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Connectors}" Visibility="{Binding Path=ShowConnectors, Converter={x:Static dd:BoolToVisibilityConverter.Instance}}">
                    <dd:ConnectorContainer.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <dd:Connector    
                                    Content="{Binding .}" 
                                    SnapsToDevicePixels="True"/>
                            </Grid>
                        </DataTemplate>
                    </dd:ConnectorContainer.ItemTemplate>
                </dd:ConnectorContainer>
            </ControlTemplate>

            <!--基础类型-->
            <Style x:Key="designerItemStyle"
                                TargetType="{x:Type ContentPresenter}">
                <Setter Property="Canvas.Left"
                                    Value="{Binding Left}" />
                <Setter Property="Canvas.Top"
                                    Value="{Binding Top}" />
                <Setter Property="Canvas.ZIndex"
                                    Value="{Binding ZIndex}" />
                <Setter Property="dd:SelectionProps.EnabledForSelection"
                                    Value="{Binding EnabledForSelection}" />
                <Setter Property="dd:ItemConnectProps.EnabledForConnection"
                                    Value="{Binding EnabledForConnection}" />
                <Setter Property="Visibility"
                                    Value="{Binding Visible,Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <Setter Property="Width"
                                    Value="{Binding ItemWidth}" />
                <Setter Property="Height"
                                    Value="{Binding ItemHeight}" />
                <Setter Property="SnapsToDevicePixels"
                                    Value="True" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid x:Name="selectedGrid" IsHitTestVisible="{Binding IsHitTestVisible}" >
                                <Grid.ContextMenu>
                                    <ContextMenu ItemsSource="{Binding MenuOptions}" Visibility="{Binding ShowMenuOptions,Converter={StaticResource BooleanToVisibilityConverter}}">
                                        <ContextMenu.ItemContainerStyle>
                                            <Style TargetType="MenuItem">
                                                <Setter Property="MenuItem.Header" Value="{Binding Text}" />
                                                <Setter Property="MenuItem.ItemsSource" Value="{Binding Children}" />
                                                <Setter Property="MenuItem.Command" Value="{Binding Command}" />
                                                <Setter Property="MenuItem.Icon" Value="{Binding Icon}" />
                                                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                                                <Setter Property="MenuItem.IsCheckable" Value="{Binding IsCheckable}" />
                                                <Setter Property="MenuItem.IsChecked" Value="{Binding IsChecked}" />
                                            </Style>
                                        </ContextMenu.ItemContainerStyle>
                                    </ContextMenu>
                                </Grid.ContextMenu>

                                <!-- PART_DragThumb -->
                                <c:DragThumb x:Name="PART_DragThumb" 
                                                            Cursor="SizeAll" >
                                    <c:DragThumb.InputBindings>
                                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                    </c:DragThumb.InputBindings>
                                </c:DragThumb>

                                <Grid RenderTransformOrigin="0.5,0.5">

                                    <!-- PART_ResizeDecorator -->
                                    <Control x:Name="PART_ResizeDecorator"
                                                 Visibility="Collapsed"
                                                 Template="{StaticResource ResizeDecoratorTemplate}"/>


                                    <!-- PART_ContentPresenter -->
                                    <ContentPresenter x:Name="PART_ContentPresenter"
                                                                HorizontalAlignment="Stretch"
                                                                VerticalAlignment="Stretch"
                                                                Content="{TemplateBinding Content}" />

                                    <Control x:Name="PART_Text" Margin="1" IsHitTestVisible="{Binding IsReadOnlyText,Converter={StaticResource InvertBoolConverter}}">
                                        <Control.Style>
                                            <Style TargetType="Control">
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate>
                                                            <Grid/>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding ShowText}" Value="True">
                                                        <Setter Property="Template">
                                                            <Setter.Value>
                                                                <ControlTemplate>
                                                                    <dd:TextControl x:Name="PART_Text" />
                                                                </ControlTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding Text,Converter={StaticResource NotNullOrEmptyToBoolConverter}}" Value="True">
                                                        <Setter Property="Template">
                                                            <Setter.Value>
                                                                <ControlTemplate>
                                                                    <dd:TextControl x:Name="PART_Text" />
                                                                </ControlTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding CustomText}" Value="True">
                                                        <Setter Property="Template">
                                                            <Setter.Value>
                                                                <ControlTemplate>
                                                                    <Grid/>
                                                                </ControlTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Control.Style>
                                    </Control>

                                    <!-- PART_ConnectorDecorator -->
                                    <Control x:Name="PART_ConnectorDecorator" Template="{StaticResource ConnectorDecoratorTemplate}"/>
                   
                                    <Grid.RenderTransform>
                                        <TransformGroup>
                                            <RotateTransform Angle="{Binding Angle}" />
                                            <ScaleTransform x:Name="scale" ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}"></ScaleTransform>
                                        </TransformGroup>
                                    </Grid.RenderTransform>

                                </Grid>
                            </Grid>
                            <DataTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected}" Value="True" />
                                        <Condition Binding="{Binding ParentId}" Value="{x:Static sys:Guid.Empty}" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected}" Value="True" />
                                        <Condition Binding="{Binding AlwayForResized}" Value="True" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
                                </MultiDataTrigger>
                                <DataTrigger Binding="{Binding Connectors.Count}" Value="0">
                                    <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsInnerConnector}" Value="True">
                                    <Setter TargetName="PART_ConnectorDecorator" Property="Template" Value="{StaticResource InnerConnectorDecoratorTemplate}" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect BlurRadius="10" Color="{Binding ColorViewModel.ShadowColor}"/>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorViewModel.ShadowColor}" Value="Transparent">
                        <Setter Property="Effect" Value="{x:Null}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <!--连接线-->
            <Style x:Key="connectorItemStyle"
                                TargetType="{x:Type ContentPresenter}">
                <Setter Property="Width"
                                    Value="{Binding Area.Width}" />
                <Setter Property="Height"
                                    Value="{Binding Area.Height}" />
                <Setter Property="Canvas.Top"
                                    Value="{Binding Area.Top}" />
                <Setter Property="Canvas.Left"
                                    Value="{Binding Area.Left}" />
                <Setter Property="Canvas.ZIndex"
                                    Value="{Binding ZIndex}" />
                <Setter Property="dd:SelectionProps.EnabledForSelection"
                                    Value="{Binding EnabledForSelection}" />
                <Setter Property="Visibility"
                                    Value="{Binding Visible,Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid x:Name="selectedGrid" IsHitTestVisible="{Binding IsHitTestVisible}">
                                <Grid.InputBindings>
                                    <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                </Grid.InputBindings>

                                <Grid.ContextMenu>
                                    <ContextMenu x:Name="PART_ContextMenu">
                                        <MenuItem Header="删除" Command="{Binding DeleteConnectionCommand}" CommandParameter="{Binding}"/>
                                        <MenuItem Header="添加文本" Command="{Binding AddLabelCommand}" CommandParameter="{Binding}"/>
                                        <MenuItem Header="插入点(按住ctrl可一直插入)" IsCheckable="True" IsChecked="{Binding ShouldInsertAnchor}" IsEnabled="{Binding IsReadOnly,Converter={dd:InvertBoolConverter}}" />
                                    </ContextMenu>
                                </Grid.ContextMenu>

                                <dd:LineControl x:Name="line"/>

                                <!-- PART_DragThumb -->
                                <c:DragThumb x:Name="PART_DragThumb" Margin="{Binding DragThumbMargin}"
                                                            Cursor="SizeAll" >
                                    <c:DragThumb.InputBindings>
                                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                    </c:DragThumb.InputBindings>
                                </c:DragThumb>

                                <dd:PointContainer x:Name="PART_VerticesContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Vertices}" Visibility="Collapsed">
                                    <dd:PointContainer.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ContextMenu>
                                                    <ContextMenu>
                                                        <MenuItem Header="删除" Command="{Binding DeleteVertexCommand}" CommandParameter="{Binding}"/>
                                                    </ContextMenu>
                                                </Grid.ContextMenu>
                                                <dd:PointConnector  Style="{StaticResource PointConnectorStyle}"/>
                                                <c:PointDragThumb Cursor="SizeAll" Opacity="0"/>
                                            </Grid>
                                        </DataTemplate>
                                    </dd:PointContainer.ItemTemplate>
                                    <dd:PointContainer.Resources>
                                        <Style TargetType="{x:Type ContentPresenter}">
                                            <Setter Property="Canvas.Left" Value="{Binding Left}" />
                                            <Setter Property="Canvas.Top" Value="{Binding Top}" />
                                        </Style>
                                    </dd:PointContainer.Resources>
                                </dd:PointContainer>

                                <dd:PointContainer x:Name="PART_LabelsContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Labels}" >
                                    <dd:PointContainer.ItemTemplate>
                                        <DataTemplate>
                                            <Grid dd:SelectionProps.EnabledForSelection="True" Background="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}">
                                                <Grid.ContextMenu>
                                                    <ContextMenu>
                                                        <MenuItem Header="删除" Command="{Binding DeleteLabelCommand}" CommandParameter="{Binding}"/>
                                                    </ContextMenu>
                                                </Grid.ContextMenu>
                                                <c:PointDragThumb Cursor="SizeAll" Opacity="0">
                                                    <c:PointDragThumb.InputBindings>
                                                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                                    </c:PointDragThumb.InputBindings>
                                                </c:PointDragThumb>
                                                <dd:TextControl Margin="5" />
                                            </Grid>
                                        </DataTemplate>
                                    </dd:PointContainer.ItemTemplate>
                                    <dd:PointContainer.Resources>
                                        <Style TargetType="{x:Type ContentPresenter}">
                                            <Setter Property="Canvas.Left" Value="{Binding Left}" />
                                            <Setter Property="Canvas.Top" Value="{Binding Top}" />
                                        </Style>
                                    </dd:PointContainer.Resources>
                                </dd:PointContainer>

                                <Rectangle Fill="#7F243859" Opacity="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="{Binding ShouldInsertAnchor, Converter={StaticResource BooleanToVisibilityConverter}}">
                                    <i:Interaction.Behaviors>
                                        <dd:ControlMouseLeftButtonDownCommandBehavior Command="{Binding AddVertexCommand}" />
                                    </i:Interaction.Behaviors>
                                </Rectangle>
                            </Grid>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                    <Setter TargetName="PART_VerticesContainer"  Property="Visibility" Value="Visible"/>
                                </DataTrigger>

                                <DataTrigger Binding="{Binding ShouldInsertAnchor}" Value="True">
                                    <Setter TargetName="PART_VerticesContainer" Property="Visibility" Value="Visible"/>
                                </DataTrigger>

                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--自带的Gif样式-->
            <Style x:Key="gifimageItemStyle"
                                TargetType="{x:Type ContentPresenter}">
                <Setter Property="Canvas.Top"
                                    Value="{Binding Top}" />
                <Setter Property="Canvas.Left"
                                    Value="{Binding Left}" />
                <Setter Property="Canvas.ZIndex"
                                    Value="{Binding ZIndex}" />
                <Setter Property="dd:SelectionProps.EnabledForSelection"
                                    Value="{Binding EnabledForSelection}" />
                <Setter Property="dd:ItemConnectProps.EnabledForConnection"
                                    Value="{Binding EnabledForConnection}" />
                <Setter Property="Visibility"
                                    Value="{Binding Visible,Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <Setter Property="Width"
                                    Value="{Binding ItemWidth}" />
                <Setter Property="Height"
                                    Value="{Binding ItemHeight}" />
                <Setter Property="SnapsToDevicePixels"
                                    Value="True" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid x:Name="selectedGrid" IsHitTestVisible="{Binding IsHitTestVisible}">
                                <Grid.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="插入点" IsCheckable="True" IsChecked="{Binding ShouldInsertAnchor}" />
                                        <MenuItem Header="选择" ItemsSource="{Binding MenuOptions}" ItemContainerStyle="{StaticResource CinchMenuItemStyle}"/>
                                    </ContextMenu>
                                </Grid.ContextMenu>
                                <!-- PART_DragThumb -->
                                <c:DragThumb x:Name="PART_DragThumb"
                                                            Cursor="SizeAll" />
                                <Grid RenderTransformOrigin="0.5,0.5">
                                    <!-- PART_ResizeDecorator -->
                                    <Control x:Name="PART_ResizeDecorator" 
                                                 Visibility="Collapsed"
                                                    Template="{StaticResource ResizeDecoratorTemplate}"/>
                                    <!-- PART_ContentPresenter -->
                                    <ContentPresenter x:Name="PART_ContentPresenter"
                                                                HorizontalAlignment="Stretch"
                                                                VerticalAlignment="Stretch"
                                                                Content="{TemplateBinding Content}" />
                                    <!-- PART_ConnectorDecorator -->
                                    <dd:ConnectorContainer x:Name="PART_ConnectorContainer" Visibility="Hidden" Style="{StaticResource GifImageConnectorContainer}"  ItemsSource="{Binding Connectors}">
                                        <dd:ConnectorContainer.ItemTemplate>
                                            <DataTemplate>
                                                <dd:Connector Content="{Binding .}" SnapsToDevicePixels="True"/>
                                            </DataTemplate>
                                        </dd:ConnectorContainer.ItemTemplate>
                                    </dd:ConnectorContainer>
                                    <Grid.RenderTransform>
                                        <TransformGroup>
                                            <RotateTransform Angle="{Binding Angle}" />
                                            <ScaleTransform x:Name="scale" ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}"></ScaleTransform>
                                        </TransformGroup>
                                    </Grid.RenderTransform>
                                </Grid>
                            </Grid>
                            <DataTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected}" Value="True" />
                                        <Condition Binding="{Binding ParentId}" Value="{x:Static sys:Guid.Empty}" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
                                </MultiDataTrigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="PART_ConnectorContainer" Property="Visibility" Value="Visible" />
                                </Trigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--自带的logical样式-->
            <Style x:Key="logicalItemStyle"
                                TargetType="{x:Type ContentPresenter}">
                <Setter Property="Canvas.Top"
                                    Value="{Binding Top}" />
                <Setter Property="Canvas.Left"
                                    Value="{Binding Left}" />
                <Setter Property="Canvas.ZIndex"
                                    Value="{Binding ZIndex}" />
                <Setter Property="dd:SelectionProps.EnabledForSelection"
                                    Value="{Binding EnabledForSelection}" />
                <Setter Property="dd:ItemConnectProps.EnabledForConnection"
                                    Value="{Binding EnabledForConnection}" />
                <Setter Property="Visibility"
                                    Value="{Binding Visible,Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <Setter Property="Width"
                                    Value="{Binding ItemWidth}" />
                <Setter Property="Height"
                                    Value="{Binding ItemHeight}" />
                <Setter Property="SnapsToDevicePixels"
                                    Value="True" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid x:Name="selectedGrid">
                                <Grid.ContextMenu>
                                    <ContextMenu ItemsSource="{Binding MenuOptions}" Visibility="{Binding ShowMenuOptions,Converter={StaticResource BooleanToVisibilityConverter}}">
                                        <ContextMenu.ItemContainerStyle>
                                            <Style TargetType="MenuItem">
                                                <Setter Property="MenuItem.Header" Value="{Binding Text}" />
                                                <Setter Property="MenuItem.ItemsSource" Value="{Binding Children}" />
                                                <Setter Property="MenuItem.Command" Value="{Binding Command}" />
                                                <Setter Property="MenuItem.Icon" Value="{Binding Icon}" />
                                                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                                                <Setter Property="MenuItem.IsCheckable" Value="{Binding IsCheckable}" />
                                                <Setter Property="MenuItem.IsChecked" Value="{Binding IsChecked}" />
                                            </Style>
                                        </ContextMenu.ItemContainerStyle>
                                    </ContextMenu>
                                </Grid.ContextMenu>

                                <!--PART_ConnectorDecorator-->
                                <dd:ConnectorContainer x:Name="PART_ConnectorContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Connectors}" Margin="0,0,0,0">
                                    <dd:ConnectorContainer.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <dd:Connector Content="{Binding .}" SnapsToDevicePixels="True"/>
                                            </Grid>
                                        </DataTemplate>
                                    </dd:ConnectorContainer.ItemTemplate>
                                </dd:ConnectorContainer>
                                <!-- PART_DragThumb -->
                                <c:DragThumb x:Name="PART_DragThumb"
                                                            Cursor="SizeAll" >
                                    <c:DragThumb.InputBindings>
                                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                    </c:DragThumb.InputBindings>
                                </c:DragThumb>
                                <!-- PART_ResizeDecorator -->
                                <Control x:Name="PART_ResizeDecorator" 
                                                 Visibility="Collapsed"
                                                    Template="{StaticResource ResizeDecoratorTemplate}"/>
                                <!-- PART_ContentPresenter -->
                                <ContentPresenter x:Name="PART_ContentPresenter"
                                                                HorizontalAlignment="Stretch"
                                                                VerticalAlignment="Stretch"
                                                                Content="{TemplateBinding Content}" />
                            </Grid>
                            <DataTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Value="True" Binding="{Binding IsSelected}"/>
                                        <Condition Value="{x:Static sys:Guid.Empty}" Binding="{Binding ParentId}"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
                                </MultiDataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Control x:Name="control">
            <Control.Style>
                <Style TargetType="Control">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid>
                                    <ItemsControl ItemsSource="{Binding Items}"
                                        ItemContainerStyleSelector="{x:Static dd:DesignerItemsControlItemStyleSelector.Instance}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <dd:DesignerCanvas
                                                    Height="{Binding PageSize.Height}"
                                                    Width="{Binding PageSize.Width}"
                                                    ShowGrid="{Binding ShowGrid}"
                                                    GridCellSize="{Binding GridCellSize}"
                                                    GridMarginSize="{Binding GridMarginSize}"
                                                    GridColor="{Binding GridColor}"
                                                    Background="{Binding PageBackground,Converter={StaticResource ColorBrushConverter}}"
                                                    AllowDrop="{Binding AllowDrop}">
                                                    <dd:DesignerCanvas.LayoutTransform>
                                                        <ScaleTransform ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}"  />
                                                    </dd:DesignerCanvas.LayoutTransform>
                                                </dd:DesignerCanvas>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                    </ItemsControl>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DefaultZoomBox}" Value="true">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Grid>
                                            <ScrollViewer Name="DesignerScrollViewer"
                                                  Background="Transparent"
                                                  HorizontalScrollBarVisibility="Auto"
                                                  VerticalScrollBarVisibility="Auto"
                                                  Focusable="False">
                                                <ItemsControl x:Name="diagram" ItemsSource="{Binding Items}"
                                                    ItemContainerStyleSelector="{x:Static dd:DesignerItemsControlItemStyleSelector.Instance}">
                                                    <ItemsControl.ItemsPanel>
                                                        <ItemsPanelTemplate>
                                                            <dd:DesignerCanvas
                                                                Height="{Binding PageSize.Height}"
                                                                Width="{Binding PageSize.Width}"
                                                                ShowGrid="{Binding ShowGrid}"
                                                                GridCellSize="{Binding GridCellSize}"
                                                                GridMarginSize="{Binding GridMarginSize}"
                                                                GridColor="{Binding GridColor}"
                                                                Background="{Binding PageBackground,Converter={StaticResource ColorBrushConverter}}"
                                                                AllowDrop="{Binding AllowDrop}">
                                                                <dd:DesignerCanvas.LayoutTransform>
                                                                    <ScaleTransform ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}"  />
                                                                </dd:DesignerCanvas.LayoutTransform>
                                                            </dd:DesignerCanvas>
                                                        </ItemsPanelTemplate>
                                                    </ItemsControl.ItemsPanel>

                                                </ItemsControl>
                                            </ScrollViewer>
                                            <dd:ZoomBox x:Name="zoomBox"
                                                Width="180"
                                                HorizontalAlignment="Left"
                                                VerticalAlignment="Bottom"
                                                DesignerCanvas="{Binding ElementName=diagram}"
                                                MaximumZoomValue="{Binding MaximumZoomValue,Mode=TwoWay}"  
                                                MinimumZoomValue="{Binding MinimumZoomValue,Mode=TwoWay}"  
                                                ZoomValue="{Binding ZoomValue,Mode=TwoWay}"
                                                FitViewModel="{Binding FitViewModel}"
                                                Margin="5,0,0,5" />
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Control.Style>
        </Control>
    </Grid>
</UserControl>
