<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:s="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"/>
            <s:ColorBrushConverter x:Key="ColorBrushConverter" />
            <s:ConectorOrientationConverter  x:Key="ConectorOrientationConverter" />
            <s:ConectorValueConverter x:Key="ConectorValueConverter"/>
            <s:ArrowPathConverter x:Key="ArrowPathConverter"/>
            <s:ArrowSizeConverter x:Key="ArrowSizeConverter"/>
            <s:LineDashConverter x:Key="LineDashConverter"/>
            <s:ClipConverter x:Key="ClipConverter"/>
            <s:InvertBoolConverter x:Key="InvertBoolConverter"/>
            <s:ConectorStyleConverter x:Key="ConectorStyleConverter"/>
            <s: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="s: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 s:PointConnector}">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type s:PointConnector}">
                            <Grid Width="{Binding ConnectorWidth}" Height="{Binding ConnectorHeight}">
                                <!-- transparent extra space makes connector easier to hit -->
                                <Ellipse Fill="Transparent" Margin="-2" />
                                <Ellipse Fill="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}"  StrokeThickness="1" Stroke="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="GifImageConnectorContainer" TargetType="{x:Type s: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>
                                        <s: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="-5">
                    <s:Connector 
                        Content="{Binding LeftConnector}" 
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="Left"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding TopLeftConnector}" 
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="TopLeft"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding TopConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="Top"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding TopRightConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="TopRight"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding RightConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="Right"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding BottomRightConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="BottomRight"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Right"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding BottomConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="Bottom"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Center"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                    <s:Connector 
                        Content="{Binding BottomLeftConnector}"
                        Cursor="Cross"
                        SnapsToDevicePixels="True"
                        Orientation="BottomLeft"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Left"
                        Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                </Grid>
            </ControlTemplate>

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

        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.LayoutTransform>
        <ScaleTransform x:Name="scale" ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}" Changed="ScaleTransform_Changed"  />
    </UserControl.LayoutTransform>
    <Border BorderBrush="LightGray"
            BorderThickness="0">
        <Grid>
            <ItemsControl ItemsSource="{Binding Items}"
                                ItemContainerStyleSelector="{x:Static s:DesignerItemsControlItemStyleSelector.Instance}">
                <ItemsControl.Resources>

                    <!--基础类型-->
                    <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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <Setter Property="s:ItemConnectProps.EnabledForConnection"
                                    Value="True" />
                        <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="5" 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>
                                                                            <s: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>
                                                                            <s: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 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>
                                        <Trigger Property="IsMouseOver"
                                                        Value="true">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/>
                                        </Trigger>
                                        <DataTrigger Value="True"
                                                            Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible" />
                                        </DataTrigger>
                                        <DataTrigger Value="0"
                                                            Binding="{Binding Connectors.Count}">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Collapsed" />
                                        </DataTrigger>
                                        <DataTrigger Value="True"
                                                            Binding="{Binding IsInnerConnector}">
                                            <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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid x:Name="selectedGrid">
                                        <Grid.InputBindings>
                                            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding EditCommand}" CommandParameter="{Binding }" />
                                        </Grid.InputBindings>

                                        <Grid.ContextMenu>
                                            <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={s:InvertBoolConverter}}" />
                                            </ContextMenu>
                                        </Grid.ContextMenu>

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

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

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

                                        <s:PointContainer x:Name="PART_LabelsContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Labels}" >
                                            <s:PointContainer.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid s: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>
                                                        <s:TextControl Margin="5" />
                                                    </Grid>
                                                </DataTemplate>
                                            </s:PointContainer.ItemTemplate>
                                            <s:PointContainer.Resources>
                                                <Style TargetType="{x:Type ContentPresenter}">
                                                    <Setter Property="Canvas.Left" Value="{Binding Left}" />
                                                    <Setter Property="Canvas.Top" Value="{Binding Top}" />
                                                </Style>
                                            </s:PointContainer.Resources>
                                        </s:PointContainer>

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

                                        <DataTrigger Value="True"
                                                            Binding="{Binding ShouldInsertAnchor}">
                                            <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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <Setter Property="s:ItemConnectProps.EnabledForConnection"
                                    Value="True" />
                        <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 -->
                                            <s:ConnectorContainer x:Name="PART_ConnectorContainer" Visibility="Hidden" Style="{StaticResource GifImageConnectorContainer}"  ItemsSource="{Binding Connectors}">
                                                <s:ConnectorContainer.ItemTemplate>
                                                    <DataTemplate>
                                                        <s:Connector Content="{Binding .}" Cursor="Cross" SnapsToDevicePixels="True"/>
                                                    </DataTemplate>
                                                </s:ConnectorContainer.ItemTemplate>
                                            </s: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 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>
                                        <Trigger Property="IsMouseOver" Value="true">
                                            <Setter TargetName="PART_ConnectorContainer" Property="Visibility" Value="Visible" />
                                        </Trigger>
                                        <DataTrigger Value="True" Binding="{Binding IsDragConnectionOver}">
                                            <Setter TargetName="PART_ConnectorContainer" Property="Visibility" Value="Visible" />
                                        </DataTrigger>
                                    </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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <Setter Property="s:ItemConnectProps.EnabledForConnection"
                                    Value="True" />
                        <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-->
                                        <s:ConnectorContainer x:Name="PART_ConnectorContainer" Style="{StaticResource ItemsControlStyle}" ItemsSource="{Binding Connectors}" Margin="0,0,0,0">
                                            <s:ConnectorContainer.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid>
                                                        <s:Connector Content="{Binding .}" Cursor="Cross" SnapsToDevicePixels="True"/>
                                                    </Grid>
                                                </DataTemplate>
                                            </s:ConnectorContainer.ItemTemplate>
                                        </s: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>

                    <Style x:Key="pointItemStyle"
                                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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <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" Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}">
                                        <!-- PART_ConnectorDecorator -->
                                        <Grid Margin="-5"
                                                    x:Name="PART_ConnectorDecorator">
                                            <s:PointConnector DataContext="{Binding TopConnector}" Style="{StaticResource PointConnectorStyle}"/>
                                        </Grid>
                                        <!-- PART_DragThumb -->
                                        <c:DragThumb x:Name="PART_DragThumb"
                                                            Cursor="SizeAll" />
                                    </Grid>
                                    <DataTemplate.Triggers>
                                        <Trigger Property="IsMouseOver"
                                                        Value="true">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/>
                                        </Trigger>
                                        <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible" />
                                        </DataTrigger>

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

                    <Style x:Key="linkpointItemStyle"
                                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="s:SelectionProps.EnabledForSelection"
                                    Value="True" />
                        <Setter Property="s:ItemConnectProps.EnabledForConnection"
                                    Value="True" />
                        <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">

                                        <!-- PART_ConnectorDecorator -->
                                        <Grid Margin="-5"
                                                    x:Name="PART_ConnectorDecorator">
                                            <s:Connector Content="{Binding Connectors[0]}"
                                                                Cursor="Cross" 
                                                                SnapsToDevicePixels="True"
                                                                VerticalAlignment="Center"
                                                                HorizontalAlignment="Center"
                                                                Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
                                        </Grid>
                                        <!-- PART_DragThumb -->
                                        <c:DragThumb x:Name="PART_DragThumb"
                                                            Cursor="SizeAll" />
                                    </Grid>
                                    <DataTemplate.Triggers>
                                        <Trigger Property="IsMouseOver"
                                                        Value="true">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/>
                                        </Trigger>
                                        <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
                                            <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible" />
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ItemsControl.Resources>

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <s:DesignerCanvas Loaded="DesignerCanvas_Loaded"
                                                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}">
                        </s:DesignerCanvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

            </ItemsControl>
            <!--<s:ZoomBox x:Name="zoomBox"
                       Width="180"
                       HorizontalAlignment="Right"
                       VerticalAlignment="Bottom"
                       Margin="0,0,5,5" />-->
        </Grid>
    </Border>
</UserControl>