绘图图弧在WPF中拖放

时间:2011-06-15 17:38:08

标签: wpf mvvm drag-and-drop diagramming mo+

我正在尝试执行拖放方法来创建图表中的关系,直接与SQL Server Management Studio图表工具相似。例如,在下图中,用户会将CustomerIDUser实体拖到Customer实体,并在两者之间创建外键关系。

所需的关键功能是,当用户按照鼠标执行拖动操作时,将绘制临时弧形路径。移动实体或关系一旦创建就不是我遇到的问题。

Entity–relationship diagram

一些参考XAML对应于上图中的实体:

<!-- Entity diagram control -->
<Grid MinWidth="10" MinHeight="10" Margin="2">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*" ></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="0" IsHitTestVisible="False" Background="{StaticResource ControlDarkBackgroundBrush}">
        <Label Grid.Row="0" Grid.Column="0" Style="{DynamicResource LabelDiagram}" Content="{Binding DiagramHeader, Mode=OneWay}" />
    </Grid>
    <ScrollViewer Grid.Row="1" Grid.Column="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Background="{StaticResource ControlBackgroundBrush}" >
        <StackPanel VerticalAlignment="Top">
            <uent:EntityDataPropertiesDiagramControl DataContext="{Binding EntityDataPropertiesFolder}" />
            <uent:CollectionEntityPropertiesDiagramControl DataContext="{Binding CollectionEntityPropertiesFolder}" />
            <uent:DerivedEntityDataPropertiesDiagramControl DataContext="{Binding DerivedEntityDataPropertiesFolder}" />
            <uent:ReferenceEntityPropertiesDiagramControl DataContext="{Binding ReferenceEntityPropertiesFolder}" />
            <uent:MethodsDiagramControl DataContext="{Binding MethodsFolder}" />
        </StackPanel>
    </ScrollViewer>
    <Grid Grid.RowSpan="2" Margin="-10">
        <lib:Connector x:Name="LeftConnector" Orientation="Left" VerticalAlignment="Center" HorizontalAlignment="Left" Visibility="Collapsed"/>
        <lib:Connector x:Name="TopConnector" Orientation="Top" VerticalAlignment="Top" HorizontalAlignment="Center" Visibility="Collapsed"/>
        <lib:Connector x:Name="RightConnector" Orientation="Right" VerticalAlignment="Center" HorizontalAlignment="Right" Visibility="Collapsed"/>
        <lib:Connector x:Name="BottomConnector" Orientation="Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Center" Visibility="Collapsed"/>
    </Grid>
</Grid>

我目前的做法是:

1)在实体的子控件中启动拖动操作,例如:

protected override void OnPreviewMouseMove(MouseEventArgs e)
{
    if (e.LeftButton != MouseButtonState.Pressed)
    {
        dragStartPoint = null;
    }
    else if (dragStartPoint.HasValue)
    {
        Point? currentPosition = new Point?(e.GetPosition(this));
        if (currentPosition.HasValue && (Math.Abs(currentPosition.Value.X - dragStartPoint.Value.X) > 10 || Math.Abs(currentPosition.Value.Y - dragStartPoint.Value.Y) > 10))
        {
            DragDrop.DoDragDrop(this, DataContext, DragDropEffects.Link);
            e.Handled = true;
        }
    }
}

2)当拖动操作离开实体时创建连接器装饰器,例如:

protected override void OnDragLeave(DragEventArgs e)
{
    base.OnDragLeave(e);
    if (ParentCanvas != null)
    {
        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(ParentCanvas);
        if (adornerLayer != null)
        {
            ConnectorAdorner adorner = new ConnectorAdorner(ParentCanvas, BestConnector);
            if (adorner != null)
            {
                adornerLayer.Add(adorner);
                e.Handled = true;
            }
        }
    }
}

3)在连接器装饰器中移动鼠标时绘制弧形路径,例如:

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (!IsMouseCaptured) CaptureMouse();
            HitTesting(e.GetPosition(this));
            pathGeometry = GetPathGeometry(e.GetPosition(this));
            InvalidateVisual();
        }
        else
        {
            if (IsMouseCaptured) ReleaseMouseCapture();
        }
    }

Canvas绑定到视图模型,Canvas上的实体和关系又绑定到各个视图模型。与整体图表相关的一些XAML

<ItemsControl ItemsSource="{Binding Items, Mode=OneWay}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <lib:DesignerCanvas VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding X}"/>
            <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            <Setter Property="Canvas.Width" Value="{Binding Width}"/>
            <Setter Property="Canvas.Height" Value="{Binding Height}"/>
            <Setter Property="Canvas.ZIndex" Value="{Binding ZIndex}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

DataTemplate s for entites and relations:

<!-- diagram relationship -->
<DataTemplate DataType="{x:Type dvm:DiagramRelationshipViewModel}">
    <lib:Connection />
</DataTemplate>
<!-- diagram entity -->
<DataTemplate DataType="{x:Type dvm:DiagramEntityViewModel}">
    <lib:DesignerItem>
        <lib:EntityDiagramControl />
    </lib:DesignerItem>
</DataTemplate>

问题问题是,一旦拖动操作开始,就不再跟踪鼠标移动,并且连接器装饰器无法像在其他上下文中那样绘制弧。如果我释放鼠标并再次单击,则弧开始绘制,但随后我丢失了源对象。我正试图找到一种方法来传递源对象和鼠标移动。

Bounty:回到此问题,我目前计划不直接使用拖放来执行此操作。我目前计划为图控件添加一个DragItem和IsDragging DependencyProperty,它将保持拖动的项目,并标记是否发生了拖动操作。然后,我可以使用DataTrigger来更改基于IsDragging的CursorAdorner可见性,并可以使用DragItem进行放置操作。

(但是,我希望在另一个有趣的方法上给予奖励。请评论是否需要更多信息或代码来澄清这个问题。)

编辑:优先级较低,但我仍在寻找更好的拖放图表方法解决方案。想要在开源Mo+解决方案构建器中实现更好的方法。

3 个答案:

答案 0 :(得分:3)

这是一个相当复杂的答案。如果有任何部分不清楚,请告诉我。

我目前正在尝试解决类似的问题。在我的例子中,我想将ListBox ItemsSource绑定到一个集合,然后将该集合中的每个项目表示为节点,即可拖动对象或连接即一行在拖动节点时重绘自身的节点之间。我将向您展示我的代码和详细信息,我认为您可能需要进行更改以满足您的需求。

拖动

通过设置Dragger类拥有的附加属性来完成拖动。在我看来,这比使用MoveThumb执行拖动更有优势,因为使对象可拖动不涉及更改其控件模板。我的第一个实现实际上在控件模板中使用了MoveThumb来实现拖动,但我发现这样做会使我的应用程序非常脆弱(添加新功能通常会打破拖延)。这是Dragger的代码:

public static class Dragger
    {
        private static FrameworkElement currentlyDraggedElement;
        private static FrameworkElement CurrentlyDraggedElement
        {
            get { return currentlyDraggedElement; } 
            set
            {
                currentlyDraggedElement = value;
                if (CurrentlyDraggedElement != null)
                {
                    CurrentlyDraggedElement.MouseMove += new MouseEventHandler(CurrentlyDraggedElement_MouseMove);
                    CurrentlyDraggedElement.MouseLeftButtonUp +=new MouseButtonEventHandler(CurrentlyDraggedElement_MouseLeftButtonUp);
                }
            }           
        }

        private static ItemPreviewAdorner adornerForDraggedItem;
        private static ItemPreviewAdorner AdornerForDraggedItem
        {
            get { return adornerForDraggedItem; }
            set { adornerForDraggedItem = value; }
        }

        #region IsDraggable

        public static readonly DependencyProperty IsDraggableProperty = DependencyProperty.RegisterAttached("IsDraggable", typeof(Boolean), typeof(Dragger),
            new FrameworkPropertyMetadata(IsDraggable_PropertyChanged));

        public static void SetIsDraggable(DependencyObject element, Boolean value)
        {
            element.SetValue(IsDraggableProperty, value);
        }
        public static Boolean GetIsDraggable(DependencyObject element)
        {
            return (Boolean)element.GetValue(IsDraggableProperty);
        }

        #endregion

        #region IsDraggingEvent

        public static readonly RoutedEvent IsDraggingEvent = EventManager.RegisterRoutedEvent("IsDragging", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(Dragger));

        public static event RoutedEventHandler IsDragging;

        public static void AddIsDraggingHandler(DependencyObject d, RoutedEventHandler handler)
        {
            UIElement uie = d as UIElement;
            if (uie != null)
            {
                uie.AddHandler(Dragger.IsDraggingEvent, handler);
            }
        }

        public static void RemoveIsDraggingEventHandler(DependencyObject d, RoutedEventHandler handler)
        {
            UIElement uie = d as UIElement;
            if (uie != null)
            {
                uie.RemoveHandler(Dragger.IsDraggingEvent, handler);
            }
        }

        #endregion

        public static void IsDraggable_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.NewValue == true)
            {
                FrameworkElement element = (FrameworkElement)obj;
                element.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(itemToBeDragged_MouseLeftButtonDown);
            }
        }

        private static void itemToBeDragged_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            var element = sender as FrameworkElement;
            if (element != null)
            {                
                CurrentlyDraggedElement = element;
            }           
        }

        private static void CurrentlyDraggedElement_MouseMove(object sender, MouseEventArgs e)
        {
            var element = sender as FrameworkElement;
            if (element.IsEnabled == true)
            {
                element.CaptureMouse();
                //RaiseIsDraggingEvent();
                DragObject(sender, new Point(Mouse.GetPosition(PavilionVisualTreeHelper.GetAncestor(element, typeof(CustomCanvas)) as CustomCanvas).X,
                    Mouse.GetPosition(PavilionVisualTreeHelper.GetAncestor(element, typeof(CustomCanvas)) as CustomCanvas).Y));
            }         
        }

        private static void CurrentlyDraggedElement_MouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            element.MouseMove -= new MouseEventHandler(CurrentlyDraggedElement_MouseMove);
            element.ReleaseMouseCapture();
            CurrentlyDraggedElement = null;
        }

        private static void DragObject(object sender, Point startingPoint)
        {
            FrameworkElement item = sender as FrameworkElement;

            if (item != null)
            {
                var canvas = PavilionVisualTreeHelper.GetAncestor(item, typeof(CustomCanvas)) as CustomCanvas;

                double horizontalPosition = Mouse.GetPosition(canvas).X - item.ActualWidth/2;
                double verticalPosition = Mouse.GetPosition(canvas).Y - item.ActualHeight/2;

                item.RenderTransform = ReturnTransFormGroup(horizontalPosition, verticalPosition);
                item.RaiseEvent(new IsDraggingRoutedEventArgs(item, new Point(horizontalPosition, verticalPosition), IsDraggingEvent));
            }
        }

        private static TransformGroup ReturnTransFormGroup(double mouseX, double mouseY)
        {
            TransformGroup transformGroup = new TransformGroup();
            transformGroup.Children.Add(new TranslateTransform(mouseX, mouseY));
            return transformGroup;
        }
    }

    public class IsDraggingRoutedEventArgs : RoutedEventArgs
    {
        public Point LocationDraggedTo { get; set;}
        public FrameworkElement ElementBeingDragged { get; set; }

        public IsDraggingRoutedEventArgs(DependencyObject elementBeingDragged, Point locationDraggedTo, RoutedEvent routedEvent)
            : base(routedEvent)
        {
            this.ElementBeingDragged = elementBeingDragged as FrameworkElement;
            LocationDraggedTo = locationDraggedTo;            
        }
    }

我认为Dragger要求对象位于CanvasCustomCanvas上,但除了懒惰之外,没有任何充分的理由。您可以轻松修改它以适用于任何Panel。 (这是在我的积压!)。

Dragger类也使用PavilionVisualTreeHelper.GetAncestor()辅助方法,它只是爬上Visual Tree寻找合适的元素。代码如下。

 /// <summary>
    /// Gets ancestor of starting element
    /// </summary>
    /// <param name="parentType">Desired type of ancestor</param>
    public static DependencyObject GetAncestor(DependencyObject startingElement, Type parentType)
    {
        if (startingElement == null || startingElement.GetType() == parentType)
            return startingElement;
        else
            return GetAncestor(VisualTreeHelper.GetParent(startingElement), parentType);
    }

使用Dragger类非常简单。只需在适当的控件的xaml标记中设置Dragger.IsDraggable = true即可。 (可选)您可以注册Dragger.IsDragging事件,该事件从被拖动的元素冒泡,以执行您可能需要的任何处理。

更新连接位置

我通知连接它需要重绘的机制有点草率,肯定需要重新寻址。

Connection包含两个FrameworkElement类型的DependencyProperties:Start和End。在PropertyChangedCallbacks中,我尝试将它们转换为DragAwareListBoxItems(我需要使它成为一个接口以获得更好的可重用性)。如果演员表演成功,我会注册DragAwareListBoxItem.ConnectionDragging事件。 (坏名字,不是我的!)。当该事件触发时,连接将重绘其路径。

DragAwareListBoxItem实际上并不知道它何时被拖动,所以有人必须告诉它。由于ListBoxItem在我的可视化树中的位置,它永远不会听到Dragger.IsDragging事件。因此,为了告诉它它被拖动,ListBox监听事件并通知相应的DragAwareListBoxItem。

我要发布ConnectionDragAwareListBoxItemListBox_IsDragging的代码,但我认为这里的可读性太大了。您可以在http://code.google.com/p/pavilion/source/browse/#hg%2FPavilionDesignerTool%2FPavilion.NodeDesigner查看该项目 或使用hg clone https://code.google.com/p/pavilion/克隆存储库。它是MIT许可下的开源项目,因此您可以根据需要调整它。作为警告,没有稳定的释放,因此它可以随时更改。

可连接性

与Connection Updating一样,我不会粘贴代码。相反,我会告诉你项目中要检查哪些类以及在每个类中查找什么。

从用户的角度来看,这是创建连接的方式。用户右键单击节点。这将打开一个上下文菜单,用户可以从中选择“创建新连接”。该选项创建一条直线,其起点根植于所选节点,其终点跟随鼠标。如果用户单击另一个节点,则会在两者之间创建连接。如果用户单击其他任何位置,则不会创建任何连接,并且该行将消失。

此过程涉及两个类。 ConnectionManager(实际上并不管理任何连接)包含附加属性。使用控件将ConnectionManager.IsConnectable属性设置为true,并将ConnectionManager.MenuItemInvoker属性设置为应该启动该进程的菜单项。此外,可视树中的某些控件必须侦听ConnectionPending路由事件。这是实际创建连接的地方。

选择菜单项后,ConnectionManager会创建一个LineAdorner。 ConnectionManager侦听LineAdorner LeftClick事件。当该事件被触发时,我执行命中测试以找到所选的控件。然后我举起ConnectionPending事件,将事件args传递给我想要创建连接的两个控件。由事件的订阅者来实际完成工作。

答案 1 :(得分:2)

如上所述,我目前的方法是不直接使用拖放,而是使用DependencyProperties和处理鼠标事件的组合来模拟拖放。

父图控件中的DependencyProperties为:

public static readonly DependencyProperty IsDraggingProperty = DependencyProperty.Register("IsDragging", typeof(bool), typeof(SolutionDiagramControl));
public bool IsDragging
{
    get
    {
        return (bool)GetValue(IsDraggingProperty);
    }
    set
    {
        SetValue(IsDraggingProperty, value);
    }
}

public static readonly DependencyProperty DragItemProperty = DependencyProperty.Register("DragItem", typeof(IWorkspaceViewModel), typeof(SolutionDiagramControl));
public IWorkspaceViewModel DragItem
{
    get
    {
        return (IWorkspaceViewModel)GetValue(DragItemProperty);
    }
    set
    {
        SetValue(DragItemProperty, value);
    }
}

IsDragging DependencyProperty用于在发生拖动时触发光标更改,例如:

<Style TargetType="{x:Type lib:SolutionDiagramControl}">
    <Style.Triggers>
        <Trigger Property="IsDragging" Value="True">
            <Setter Property="Cursor" Value="Pen" />
        </Trigger>
    </Style.Triggers>
</Style>

无论我需要执行drag and drop的圆弧绘图形式,而不是调用DragDrop.DoDragDrop,我都会将IsDragging = trueDragItem设置为被拖动的源项目。

在鼠标离开的实体控件中,启用了在拖动过程中绘制圆弧的连接器装饰器,例如:

protected override void OnMouseLeave(MouseEventArgs e)
{
    base.OnMouseLeave(e);
    if (ParentSolutionDiagramControl.DragItem != null)
    {
        CreateConnectorAdorner();
    }
}

图控件必须在拖动过程中处理其他鼠标事件,例如:

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    if (e.LeftButton != MouseButtonState.Pressed)
    {
        IsDragging = false;
        DragItem = null;
    }
}

图控件还必须处理鼠标按下事件时的“丢弃”(并且它必须根据鼠标位置找出正在放置的实体),例如:

protected override void OnMouseUp(MouseButtonEventArgs e)
{
    base.OnMouseUp(e);
    if (DragItem != null)
    {
        Point currentPosition = MouseUtilities.GetMousePosition(this);
        DiagramEntityViewModel diagramEntityView = GetMouseOverEntity(currentPosition );
        if (diagramEntityView != null)
        {
            // Perform the drop operations
        }
    }
    IsDragging = false;
    DragItem = null;
}

我仍在寻找一种更好的解决方案,可以在拖动操作过程中在图表上绘制临时弧(跟随鼠标)。

答案 2 :(得分:1)

我想你会想看看WPF Thumb控件。它在一个方便的包中包含了一些这样的功能。

以下是MSDN文档:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb.aspx

以下是一个例子:

http://denisvuyka.wordpress.com/2007/10/13/wpf-draggable-objects-and-simple-shape-connectors/

不幸的是,我在这个领域没有很多经验,但我确实认为这就是你要找的东西。祝你好运!