画布 WPF 中的鼠标移动用户控件

本文关键字:鼠标 移动用户控件 WPF 画布 | 更新日期: 2023-09-27 18:35:10

所以,我的问题是我试图在画布中移动一些用户控件。

这实际上效果很好,只要鼠标指针在 dockpanel 内,这就是用户控件的组成。但是,在 dockpanel 内部,有几个 itemscontrols,如果我单击它们并尝试移动它,则会生成一个异常,指出类似"无法将类型为"System.String"的对象转换为键入"UMLDesigner.Model.Node"。这是有道理的,但是有没有办法获取DockPanel,而不是Items控件,即使单击的是项目控件?

有相关的 C# 代码:

public void MouseMoveNode(MouseEventArgs e)
    {
        //Is the mouse captured?
        if (Mouse.Captured != null)
        {
            FrameworkElement movingClass = (FrameworkElement)e.MouseDevice.Target;
            Node movingNode = (Node)movingClass.DataContext;
            Canvas canvas = FindParent<Canvas>(movingClass);
            Point mousePosition = Mouse.GetPosition(canvas);
            if (moveNodePoint == default(Point)) moveNodePoint = mousePosition;
            movingNode.X = (int)mousePosition.X;
            movingNode.Y = (int)mousePosition.Y;
        }
    }
    public void MouseUpNode(MouseEventArgs e)
    {
        //Used to move node
        FrameworkElement movingClass = (FrameworkElement)e.MouseDevice.Target;
        Node movingNode = (Node)movingClass.DataContext;
        Canvas canvas = FindParent<Canvas>(movingClass);
        Point mousePosition = Mouse.GetPosition(canvas);
           new MoveNodeCommand(movingNode, (int)mousePosition.X, (int)mousePosition.Y,     (int)moveNodePoint.X, (int)moveNodePoint.Y);
        moveNodePoint = new Point();
        e.MouseDevice.Target.ReleaseMouseCapture(); 
    }

以及某些用户控件的 xaml:

<DockPanel.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0.0">
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="Azure" Offset="0"/>
                    <GradientStop Color="Transparent" Offset="1"/>
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </DockPanel.Background>
        <TextBox Text="{Binding ClassName}" HorizontalAlignment="Center"     DockPanel.Dock="Top" Margin="5,0,5,0"/>
        <!--Note the " : " is acutally being written to the GUI-->
        <ItemsControl  Name="attributeList" ItemsSource="{Binding Attributes}" Margin="5,0,5,0" DockPanel.Dock="Top">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock><Run Text="{Binding Path=.}"/> : <Run Text="Type her"/></TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <ItemsControl Name="propertiesList" ItemsSource="{Binding Properties}" Margin="5,0,5,0" DockPanel.Dock="Top">
        </ItemsControl>
        <ItemsControl Name="methodsList" ItemsSource="{Binding Methods}" Margin="5,0,5,0" DockPanel.Dock="Top">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock><Run Text="{Binding Path=.}"/>() : <Run Text="Type her"/></TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl> 
    </DockPanel>

我当然也想知道是否有更聪明或更好的方法来做到这一点。

画布 WPF 中的鼠标移动用户控件

IsHitTestVisible="False"添加到 ItemsControl。