解决Nodify框架因自带放大缩小、平移功能导致拖拽添加的控件无法准确在鼠标放下的位置显示控件

5 篇文章 0 订阅

ViewModel中写具体关键的几段代码:

var editor = sender as NodifyEditor;              

     Point p = e.GetPosition(editor);
     //放大缩小比例
     double scale = editor.ViewportZoom;

     //经过放大缩小、平移后获得坐标点位置
     p = new Point(Math.Round((p.X - editor.ViewportTransform.Value.OffsetX) / scale,0), Math.Round((p.Y - editor.ViewportTransform.Value.OffsetY) / scale,0));

详细代码如下

CustomInterfaceView.xaml

<UserControl x:Class="Ueyes.Intergration.AgingEditor.View.CustomInterfaceView"
             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:local="clr-namespace:Ueyes.Intergration.AgingEditor" 
             xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             xmlns:nodify="https://miroiu.github.io/nodify" 
             xmlns:shared="clr-namespace:Ueyes.Intergration.AgingEditor.Share;assembly=Ueyes.Intergration.AgingEditor.Share" 
             xmlns:h="http://schemas.microsoft.com/xaml/behaviors"
             mc:Ignorable="d" Name="custom"
             d:DesignHeight="800" d:DesignWidth="1000">
    <UserControl.Resources>
        <shared:TargetTypeToVisibilityConverter x:Key="TargetTypeToVisibilityConverter"/>
        <shared:BindingProxy x:Key="EditorProxy" DataContext="{Binding}" />
        <shared:BindingProxy x:Key="BlackboardProxy" DataContext="{Binding SelectedState.Action}" />

        <DataTemplate x:Key="ToolBoxStyle">
            <Grid Margin="2" Background="Transparent">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <iconPacks:PackIconMaterial x:Name="icon" Kind="Ellipse" BorderThickness="1" VerticalAlignment="Stretch" Width="24" HorizontalAlignment="Stretch"/>
                <TextBlock x:Name="txt" Grid.Column="1" Text="{Binding TypeName}" Margin="4"/>
            </Grid>

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding TypeName}" Value="Button">
                    <Setter Property="Kind" TargetName="icon" Value="GestureTapButton"/>
                    <Setter Property="Text" TargetName="txt" Value="{Binding Header}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding TypeName}" Value="TextBox">
                    <Setter Property="Kind" TargetName="icon" Value="Numeric8Box"/>
                    <Setter Property="Text" TargetName="txt" Value="{Binding Header}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding TypeName}" Value="TextBlock">
                    <Setter Property="Kind" TargetName="icon" Value="FormatText"/>
                    <Setter Property="Text" TargetName="txt" Value="{Binding Header}"/>
                </DataTrigger>       
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate x:Key="ButtonTemplate">
            <Button Command="{Binding DataContext.ClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding .}">
                <Button.Content>
                    <shared:EditableTextBlock IsEditable="{Binding IsEditable}"
                                    IsEditing="{Binding IsRenaming}" Text="{Binding Name}" />
                </Button.Content>
            </Button>
        </DataTemplate>

        <DataTemplate x:Key="TextBlockTemplate">
            <shared:EditableTextBlock IsEditable="{Binding IsEditable}" Foreground="Black" MaxLength="30"
                 IsEditing="{Binding IsRenaming}" Text="{Binding Name}" />
        </DataTemplate>

        <DataTemplate x:Key="TextBoxTemplate">
            <TextBox Text="{Binding Name}" IsEnabled="{Binding IsEditable}" MinHeight="25" MaxLength="30"/>
        </DataTemplate>

    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <nodify:NodifyEditor x:Name="Editor"  Grid.Column="1" Background="White" 
                             ItemsSource="{Binding States}" AllowDrop="True"
                             SelectedItem="{Binding SelectedState}" DisableZooming="False" DisablePanning="False"
                             SelectedItems="{Binding SelectedStates}">
            <h:Interaction.Triggers>
                <h:EventTrigger EventName="Drop">
                    <h:CallMethodAction TargetObject="{Binding}"
                                        MethodName="nodify_Drop"/>
                  
                </h:EventTrigger>
            </h:Interaction.Triggers>
            
            <nodify:NodifyEditor.ItemTemplate>
                <DataTemplate DataType="{x:Type local:StateViewModel}">

                    <nodify:StateNode Anchor="{Binding Anchor, Mode=OneWayToSource}" Padding="10 12"
                                    Content="{Binding}" IsConnected="False" ToolTip="{Binding Location}">
                        <nodify:StateNode.ContentTemplate>
                            <DataTemplate DataType="{x:Type local:StateViewModel}">
                                <ContentControl Content="{Binding}" >
                                    <ContentControl.Style>
                                        <Style TargetType="{x:Type ContentControl}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding TargetType}" Value="Button">
                                                    <Setter Property="ContentTemplate" Value="{StaticResource ButtonTemplate}" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding TargetType}" Value="TextBlock">
                                                    <Setter Property="ContentTemplate" Value="{StaticResource TextBlockTemplate}" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding TargetType}" Value="TextBox">
                                                    <Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </nodify:StateNode.ContentTemplate>
                        <nodify:StateNode.Style>
                            <Style BasedOn="{StaticResource {x:Type nodify:StateNode}}" TargetType="{x:Type nodify:StateNode}">
                                <Setter Property="BorderThickness" Value="0"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding TargetType}" Value="Button">
                                        <Setter Property="Background" Value="White" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding TargetType}" Value="TextBlock">
                                        <Setter Property="Foreground" Value="Black"/>
                                        <Setter Property="Background" Value="White" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding TargetType}" Value="TextBox">
                                        <Setter Property="Background" Value="White" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </nodify:StateNode.Style>
                    </nodify:StateNode>

                </DataTemplate>
            </nodify:NodifyEditor.ItemTemplate>

            <nodify:NodifyEditor.ItemContainerStyle>
                <Style BasedOn="{StaticResource {x:Type nodify:ItemContainer}}" TargetType="{x:Type nodify:ItemContainer}">
                    <Setter Property="BorderBrush" Value="Transparent" />
                    <Setter Property="Location" Value="{Binding Location}" />
                    <Setter Property="ActualSize" Value="{Binding Size, Mode=OneWayToSource}" />
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu DataContext="{Binding DataContext, Source={StaticResource EditorProxy}}">
                                <MenuItem
                                    Command="{Binding DeleteCommand}"
                                    Header="_Delete"
                                    Icon="{StaticResource DeleteIcon}" 
                                    InputGestureText="Delete"/>
                                <MenuItem
                                    Command="{Binding CopyCommand}"
                                    Header="_Copy"
                                    Icon="{StaticResource CopyIcon}"
                                    InputGestureText="Ctrl+D" />
                                <MenuItem
                                    Command="{Binding RenameCommand}"
                                    Header="_Rename"
                                    Icon="{StaticResource RenameIcon}" />
                                <MenuItem
                                    Command="{Binding EditableCommand}"
                                    Header="_IsEditable"
                                    Icon="{StaticResource EditIcon}" />
                                <!--<MenuItem
                                    Command="{Binding MoveCommand}"
                                    Header="_IsMove"
                                    Icon="{StaticResource EditIcon}" />-->
                                <MenuItem Header="_Alignment" Icon="{StaticResource AlignTopIcon}">
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Top"
                                        Header="_Top"
                                        Icon="{StaticResource AlignTopIcon}" />
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Left"
                                        Header="_Left"
                                        Icon="{StaticResource AlignLeftIcon}" />
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Bottom"
                                        Header="_Bottom"
                                        Icon="{StaticResource AlignBottomIcon}" />
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Right"
                                        Header="_Right"
                                        Icon="{StaticResource AlignRightIcon}" />
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Middle"
                                        Header="_Middle"
                                        Icon="{StaticResource AlignMiddleIcon}" />
                                    <MenuItem
                                        Command="{x:Static nodify:EditorCommands.Align}"
                                        CommandParameter="Center"
                                        Header="_Center"
                                        Icon="{StaticResource AlignCenterIcon}" />
                                </MenuItem>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </nodify:NodifyEditor.ItemContainerStyle>

            <nodify:NodifyEditor.ContextMenu>
                <ContextMenu DataContext="{Binding DataContext, Source={StaticResource EditorProxy}}">
                    <MenuItem
                        Command="{Binding AddButtonCommand}"
                        CommandParameter="{Binding PlacementTarget.MouseLocation, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                        Header="_Add Button"
                        Icon="{StaticResource AddStateIcon}"/>
                    <MenuItem
                        Command="{Binding AddTextBlockCommand}"
                        CommandParameter="{Binding PlacementTarget.MouseLocation, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                        Header="_Add TextBlock"
                        Icon="{StaticResource AddStateIcon}"/>
                    <MenuItem
                        Command="{Binding AddTextBoxCommand}"
                        CommandParameter="{Binding PlacementTarget.MouseLocation, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                        Header="_Add TextBox"
                        Icon="{StaticResource AddStateIcon}"/>
                    <MenuItem
                        Command="{Binding DeleteCommand}"
                        Header="_Delete"
                        Icon="{StaticResource DeleteIcon}"
                        InputGestureText="Delete" />
                    <Separator Background="{DynamicResource BorderBrush}" />
                    <MenuItem
                        Command="{x:Static nodify:EditorCommands.SelectAll}"
                        Header="_Select All"
                        Icon="{StaticResource SelectAllIcon}"
                        InputGestureText="Ctrl+A" />
                </ContextMenu>
            </nodify:NodifyEditor.ContextMenu>

            <!--快捷键添加删除新模块操作-->
            <nodify:NodifyEditor.InputBindings>
                <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" />
                <!--<KeyBinding
                        Key="A"
                        Command="{Binding AddStateCommand}"
                        CommandParameter="{Binding MouseLocation, RelativeSource={RelativeSource AncestorType={x:Type nodify:NodifyEditor}}}"
                        Modifiers="Shift" />-->
                <KeyBinding Command="{Binding CopyCommand}" 
                         Key="D" Modifiers="Ctrl"/>
                <KeyBinding Command="{x:Static nodify:EditorCommands.SelectAll}" 
                         Key="A" Modifiers="Ctrl"/>
            </nodify:NodifyEditor.InputBindings>

        </nodify:NodifyEditor>

        <Expander
               Padding="0,1,4,3"
               HorizontalAlignment="Left"
               HorizontalContentAlignment="Left"
               VerticalContentAlignment="Center"
               Background="{DynamicResource PanelBackgroundBrush}"
               ExpandDirection="Left"
               IsExpanded="True"
               Visibility="Visible">
            <Expander.Style>
                <Style BasedOn="{StaticResource {x:Type Expander}}" TargetType="{x:Type Expander}">
                    <Setter Property="Tag" Value="{StaticResource ExpandRightIcon}" />
                    <Style.Triggers>
                        <Trigger Property="IsExpanded" Value="True">
                            <Setter Property="Tag" Value="{StaticResource ExpandLeftIcon}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Expander.Style>

            <Border
                 Width="450"
                 Padding="10"
                 HorizontalAlignment="Stretch"
                 BorderBrush="{DynamicResource BackgroundBrush}"
                 BorderThickness="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="3*"/>
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>

                    <!--  TRANSITIONS  -->
                    <Grid
                 Grid.Row="0"
                 Margin="0,10,0,10"                        
                 Visibility="{Binding SelectedState, Converter={shared:BooleanToVisibilityConverter Negate=True}}">

                        <Grid VerticalAlignment="Top">
                            <ListBox x:Name="CtlList" ItemsSource="{Binding ThumbList}" Background="Transparent" 
                                     ItemTemplate="{StaticResource ToolBoxStyle}" SelectedItem="{Binding ThumbSelectedItem}" BorderThickness="0">
                                <h:Interaction.Triggers>
                                    <h:EventTrigger EventName="PreviewMouseMove">
                                        <h:CallMethodAction MethodName="CtlList_PreviewMouseMove"
                                                            TargetObject="{Binding ElementName=custom,Path=DataContext}"/>
                                    </h:EventTrigger>
                                </h:Interaction.Triggers>
                            </ListBox>
                            <!--<ItemsControl ItemsSource="{Binding ThumbList}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border Margin="5,3" Tag="{Binding TypeName}">
                                            <StackPanel Orientation="Horizontal">
                                                <iconPacks:PackIconMaterial x:Name="icon" Kind="GestureTapButton" BorderThickness="1" 
                                                                            Width="30" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                                <TextBlock x:Name="txt" Grid.Column="1" Text="{Binding Header}" Margin="4"/>
                                            </StackPanel>
                                            <h:Interaction.Triggers>
                                                <h:EventTrigger EventName="MouseLeftButtonDown">
                                                    <h:CallMethodAction MethodName="Border_MouseLeftButtonDown"
                                                                        TargetObject="{Binding ElementName=custom,Path=DataContext}"/>
                                                </h:EventTrigger>
                                            </h:Interaction.Triggers>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>-->
                        </Grid>
                    </Grid>

                    <!--  STATES  -->
                    <Grid Grid.Row="0" Grid.RowSpan="2" Margin="0,10,0,10" Visibility="{Binding SelectedState, Converter={shared:BooleanToVisibilityConverter}}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <!--  STATE NAME  -->
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <shared:EditableTextBlock
                                     FontSize="16"
                                     FontWeight="Bold"
                                     Foreground="{DynamicResource ForegroundBrush}"
                                     IsEditable="{Binding SelectedState.IsEditable}"
                                     IsEditing="{Binding IsChecked, ElementName=EditStateName}"
                                     MaxLength="20"
                                     Text="{Binding SelectedState.Name}" />
                            <CheckBox
                                    x:Name="EditStateName"
                                    Grid.Column="1"
                                    Content="{StaticResource EditIcon}"
                                    Style="{StaticResource IconCheckBox}"
                                    Visibility="{Binding SelectedState.IsEditable, Converter={shared:BooleanToVisibilityConverter}}" />
                        </Grid>

                        <Separator
                              Grid.Row="1"
                              Width="Auto"
                              Height="2"
                              Margin="0,2,0,10"/>

                        <ScrollViewer
                              Grid.Row="2"
                              VerticalScrollBarVisibility="Auto"
                              Visibility="{Binding SelectedState.Action, Converter={shared:BooleanToVisibilityConverter}}">
                            <Grid IsSharedSizeScope="True">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <!--  ACTION  -->
                                <TextBlock
                                     Margin="0,0,10,0"
                                     VerticalAlignment="Center"
                                     Text="操作" />
                                <ComboBox
                                     Grid.Column="1"
                                     DisplayMemberPath="Name"
                                     IsEnabled="{Binding SelectedState.IsEditable}"
                                     ItemsSource="{Binding Blackboard.Actions}"
                                     SelectedItem="{Binding SelectedState.ActionReference}" 
                                     />

                                <!--  INPUT  -->
                                <Expander
                                     Grid.Row="1"
                                     Grid.ColumnSpan="2"
                                     Margin="0,5,0,0"
                                     Padding="0,5,0,0"
                                     BorderBrush="{DynamicResource BackgroundBrush}"
                                     BorderThickness="0,0,0,1"
                                     FontWeight="Bold"
                                     Header="Input"
                                     IsExpanded="True"
                                     Visibility="{Binding SelectedState.Action.Input.Count, Converter={shared:BooleanToVisibilityConverter}}">
                                    <Expander.Style>
                                        <Style BasedOn="{StaticResource {x:Type Expander}}" TargetType="{x:Type Expander}">
                                            <Setter Property="Tag" Value="{StaticResource ExpandRightIcon}" />
                                            <Style.Triggers>
                                                <Trigger Property="IsExpanded" Value="True">
                                                    <Setter Property="Tag" Value="{StaticResource ExpandDownIcon}" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Expander.Style>
                                    <ItemsControl FontWeight="Normal" ItemsSource="{Binding SelectedState.Action.Input}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate DataType="{x:Type local:BlackboardKeyViewModel}">
                                                <local:BlackBoardKeyEditorView Margin="0,0,0,2">
                                                    <local:BlackBoardKeyEditorView.DataContext>
                                                        <MultiBinding Converter="{local:BlackboardKeyEditorConverter CanChangeInputType=True}">
                                                            <Binding Path="DataContext.Keys" Source="{StaticResource BlackboardProxy}" />
                                                            <Binding BindsDirectlyToSource="True" />
                                                        </MultiBinding>
                                                    </local:BlackBoardKeyEditorView.DataContext>
                                                </local:BlackBoardKeyEditorView>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Expander>
                                <!--  OUTPUT  -->
                                <Expander
                                      Grid.Row="2"
                                      Grid.ColumnSpan="2"
                                      Margin="0,5,0,0"
                                      Padding="0,5,0,0"
                                      BorderBrush="{DynamicResource BackgroundBrush}"
                                      BorderThickness="0,0,0,1"
                                      FontWeight="Bold"
                                      Header="Output"
                                      IsExpanded="True"
                                      Visibility="{Binding SelectedState.Action.Output.Count, Converter={shared:BooleanToVisibilityConverter}}">
                                    <Expander.Style>
                                        <Style BasedOn="{StaticResource {x:Type Expander}}" TargetType="{x:Type Expander}">
                                            <Setter Property="Tag" Value="{StaticResource ExpandRightIcon}" />
                                            <Style.Triggers>
                                                <Trigger Property="IsExpanded" Value="True">
                                                    <Setter Property="Tag" Value="{StaticResource ExpandDownIcon}" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Expander.Style>
                                    <ItemsControl FontWeight="Normal" ItemsSource="{Binding SelectedState.Action.Output}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate DataType="{x:Type local:BlackboardKeyViewModel}">
                                                <local:BlackBoardKeyEditorView Margin="0,0,0,2">
                                                    <local:BlackBoardKeyEditorView.DataContext>
                                                        <MultiBinding Converter="{local:BlackboardKeyEditorConverter CanChangeInputType=False}">
                                                            <Binding Path="DataContext.Keys" Source="{StaticResource BlackboardProxy}" />
                                                            <Binding BindsDirectlyToSource="True" />
                                                        </MultiBinding>
                                                    </local:BlackBoardKeyEditorView.DataContext>
                                                </local:BlackBoardKeyEditorView>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Expander>

                                <Grid Grid.Row="3" Grid.ColumnSpan="2" Margin="0 50 0 0" IsSharedSizeScope="True">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <shared:EditableTextBlock
                                                  FontSize="16"
                                                  FontWeight="Bold"
                                                  Foreground="{DynamicResource ForegroundBrush}"
                                                  IsEditing="{Binding IsChecked, ElementName=EditName}"
                                                  MaxLength="20"
                                                  Text="{Binding Name}" />

                                        <StackPanel Grid.Column="1" Orientation="Horizontal">
                                            <CheckBox
                                                 x:Name="EditName"
                                                 Content="{StaticResource EditIcon}"
                                                 Style="{StaticResource IconCheckBox}"
                                                 ToolTip="修改参数名称" />
                                            <Button
                                                 Command="{Binding SelectedState.Action.AddKeyCommand}"
                                                 Content="{StaticResource AddKeyIcon}"
                                                 Style="{StaticResource IconButton}"
                                                 ToolTip="添加新参数" />
                                        </StackPanel>
                                    </Grid>

                                    <Separator
                                          Grid.Row="1"
                                          Width="Auto"
                                          Height="2"
                                          Margin="0,2,0,10" />

                                    <ScrollViewer
                                          Grid.Row="2"
                                          HorizontalScrollBarVisibility="Auto"
                                          VerticalScrollBarVisibility="Auto">
                                        <ItemsControl ItemsSource="{Binding SelectedState.Action.ConfigParameter}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate DataType="{x:Type local:BlackboardKeyViewModel}">
                                                    <Grid Margin="0,0,0,2">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition />
                                                            <ColumnDefinition SharedSizeGroup="Actions" />
                                                        </Grid.ColumnDefinitions>

                                                        <local:BlackBoardKeyEditorView>
                                                            <local:BlackBoardKeyEditorView.DataContext>
                                                                <MultiBinding Converter="{local:BlackboardKeyEditorConverter CanChangeInputType=False}">
                                                                    <Binding Path="DataContext.ConfigParameter" Source="{StaticResource BlackboardProxy}" />
                                                                    <Binding BindsDirectlyToSource="True" />
                                                                </MultiBinding>
                                                            </local:BlackBoardKeyEditorView.DataContext>
                                                        </local:BlackBoardKeyEditorView>

                                                        <StackPanel Grid.Column="3" Orientation="Horizontal">
                                                            <CheckBox
                                                                 x:Name="EditKeyName"
                                                                 Content="{StaticResource EditIcon}"
                                                                 Style="{StaticResource IconCheckBox}"
                                                                 ToolTip="修改参数名称" />
                                                            <Button
                                                                 Command="{Binding DataContext.SelectedState.Action.RemoveKeyCommand, Source={StaticResource EditorProxy}}"
                                                                 CommandParameter="{Binding}"
                                                                 Content="{StaticResource RemoveKeyIcon}"
                                                                 Style="{StaticResource IconButton}" />
                                                        </StackPanel>
                                                    </Grid>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </ScrollViewer>
                                </Grid>





                            </Grid>
                        </ScrollViewer>


                        <ScrollViewer
                              Grid.Row="2"
                              VerticalScrollBarVisibility="Auto"
                              Visibility="{Binding SelectedState.TargetType, Converter={StaticResource TargetTypeToVisibilityConverter}}">
                            <Grid IsSharedSizeScope="True" VerticalAlignment="Top">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="编号" Margin="0,5,10,0"/>
                                <TextBox Grid.Column="1" Height="25" Text="{Binding SelectedState.Code}" IsEnabled="False"/>
                            </Grid>
                        </ScrollViewer>


                    </Grid>


                    <!-- Export/Import -->
                    <Grid Grid.Row="2">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <!--  NAME  -->

                        <TextBlock
                     FontSize="16"
                     FontWeight="Bold"
                     Foreground="{DynamicResource ForegroundBrush}"
                     Text="方案导入/导出" />

                        <Separator
                     Grid.Row="1"
                     Width="Auto"
                     Height="2"
                     Margin="0,2,0,5" />

                        <Border Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top"
                     Background="{DynamicResource PanelBackgroundBrush}"
                     BorderThickness="0,0,0,1"
                     CornerRadius="2"
                     Height="40">
                            <StackPanel Orientation="Horizontal" Height="40">
                                <Button Margin="10,0,10,0" Command="{Binding SaveCommand}"                    
                                 Content="{StaticResource SaveIcon}"
                                 Style="{StaticResource IconButton}"
                                 ToolTip="Save" Cursor="Hand" Height="40"/>
                                <Button Margin="10,0,10,0" Command="{Binding ExportSchemeCommand}"                    
                                 Content="{StaticResource ExportIcon}"
                                 Style="{StaticResource IconButton}"
                                 ToolTip="Export" Cursor="Hand" Height="40"/>
                                <Button Margin="10,0,10,0" Command="{Binding ImportSchemeCommand}"                    
                                 Content="{StaticResource ImportIcon}"
                                 Style="{StaticResource IconButton}"
                                 ToolTip="Import" Cursor="Hand" Height="40"/>
                            </StackPanel>
                        </Border>
                    </Grid>
                </Grid>
            </Border>
        </Expander>
    </Grid>
</UserControl>

 精炼重要点为

<h:Interaction.Triggers>
    <h:EventTrigger EventName="Drop">
        <h:CallMethodAction TargetObject="{Binding}"
                            MethodName="nodify_Drop"/>
    </h:EventTrigger>
</h:Interaction.Triggers>


<ListBox x:Name="CtlList" ItemsSource="{Binding ThumbList}" Background="Transparent" 
         ItemTemplate="{StaticResource ToolBoxStyle}" SelectedItem="{Binding ThumbSelectedItem}" BorderThickness="0">
    <h:Interaction.Triggers>
        <h:EventTrigger EventName="PreviewMouseMove">
            <h:CallMethodAction MethodName="CtlList_PreviewMouseMove"
                                TargetObject="{Binding ElementName=custom,Path=DataContext}"/>
        </h:EventTrigger>
    </h:Interaction.Triggers>
</ListBox>

 其中Nodify的属性可参考此链接查看

第八节:Nodify 编辑器属性-CSDN博客

里面有编辑器的一些简介

CustomInterfaceViewModel.cs

//可通过拖拽复制控件
public void CtlList_PreviewMouseMove(object sender, MouseEventArgs e)
{
    if (ThumbSelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
    {
        DragDrop.DoDragDrop((DependencyObject)sender, ThumbSelectedItem, DragDropEffects.Copy);
    }
}


//nodify 放大缩小、平移后鼠标放在哪个位置  控件就显示在哪个位置
//最重要的就是方法内前四句代码
//p = new Point(Math.Round((p.X - editor.ViewportTransform.Value.OffsetX) / scale,0), Math.Round((p.Y - editor.ViewportTransform.Value.OffsetY) / scale,0));

public void nodify_Drop(object sender, DragEventArgs e)
{
    var editor = sender as NodifyEditor;              

    Point p = e.GetPosition(editor);

    double scale = editor.ViewportZoom;

    p = new Point(Math.Round((p.X - editor.ViewportTransform.Value.OffsetX) / scale,0), Math.Round((p.Y - editor.ViewportTransform.Value.OffsetY) / scale,0));

    if (ThumbSelectedItem.TypeName == "Button")
    {
        States.Add(new CustomViewModel()
        {
            Name = "New Button",
            IsRenaming = true,
            Location = p,
            ActionReference = Blackboard.Actions.Count > 0 ? Blackboard.Actions[0] : null,
            TargetType = ThumbSelectedItem.TypeName
        });
    }
    else if (ThumbSelectedItem.TypeName == "TextBox")
    {
        States.Add(new CustomViewModel
        {
            Code = GenerateUniqueRandomCode(1, 100),
            Name = "New TextBox",
            IsRenaming = true,
            Location = p,
            TargetType = "TextBox"
        });
    }
    else
    {
        States.Add(new CustomViewModel
        {
            Name = "New TextBlock",
            IsRenaming = true,
            Location = p,
            TargetType = "TextBlock"
        });
    }
    e.Handled = true;

}


示例图

Nodify添加拖拽事件,经过放大缩小、平移后获得控件坐标点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

创可贴治愈心灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值