Question

So, my problem is that i'm trying to move some usercontrols around in a canvas.

This actually works very well, as long as the mousepointer is inside the dockpanel, which is what the usercontrol is made of. However, inside the dockpanel, there are several itemscontrols, and if I click on those, and try to move it, an exception is generated, stating something like "Unable to cast object of type "System.String" to type "UMLDesigner.Model.Node". This makes sense, but is there a way to get the DockPanel, instead of the Itemscontrol, even though it is the itemscontrol that is clicked?

There is the relevant C# code:

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(); 
    }

And the xaml for some of the usercontrol:

<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>

I would of course also like to know if there is a smarter or better way of doing this.

Was it helpful?

Solution

Add IsHitTestVisible="False" to ItemsControl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top