asusralis
Topic Author
Posts: 142
Joined: 30 Jul 2018, 05:03

Issues with DragAdornerBehavior

07 Mar 2023, 17:04

I'm trying to have something follow the mouse while I am dragging, but there are two issues:

1): It seems that the drag only works while the cursor is under another control. If the cursor is not under another control, the control stops following the cursor.

2): The control that follows the cursor is far away from the cursor.

Video: https://files.catbox.moe/n240jv.mp4

This is in a UserControl that is the size of the entire screen.
<Grid>
        <b:Interaction.Behaviors>
            <noesis:DragAdornerBehavior x:Name="DragAdorner" />
        </b:Interaction.Behaviors>
        ...
        <Border Width="25" Height="25">
            <Border.RenderTransform>
                <TranslateTransform X="{Binding DraggedItemX, ElementName=DragAdorner}" Y="{Binding DraggedItemY, ElementName=DragAdorner}" />
            </Border.RenderTransform>
            <Image Source="{Binding DraggedCharacter.Icon}" />
        </Border>
    </Grid>
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Issues with DragAdornerBehavior

09 Mar 2023, 13:42

1. The drag operation needs a hit-testable element (at least with a Transparent background) to receive the drag events. So the Grid with the DragAdornerBehavior should have a Background="Transparent" set. Although you can set it only during the drag operation if you still want to click through into the 3D scene behind, something like this:
    public void OnDragStart(object param)
    {
        _dragAdornerGrid.Background = Brushes.Transparent;
        // ...
    }
    
    public void OnDrop(object param)
    {
        // ...
        _dragAdornerGrid.Background = null;
    }
2. The element that is being moved around in your xaml is centered in the Grid, so that center offset is added to the DraggedItemX/Y coordinates. Just set HorizontalAlignment="Left" and VerticalAlignment="Top" on the Border and it will move with the mouse:
    <Grid>
        <b:Interaction.Behaviors>
            <noesis:DragAdornerBehavior x:Name="DragAdorner" />
        </b:Interaction.Behaviors>
        ...
        <Border Width="25" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center">
            <Border.RenderTransform>
                <TranslateTransform X="{Binding DraggedItemX, ElementName=DragAdorner}" Y="{Binding DraggedItemY, ElementName=DragAdorner}" />
            </Border.RenderTransform>
            <Image Source="{Binding DraggedCharacter.Icon}" />
        </Border>
    </Grid>

Who is online

Users browsing this forum: Google [Bot] and 80 guests