TranslateTransform for drag and drop in Silverlight

We are trying to implement drag and drop in Silverlight (3). We want users to be able to drag items from the tree to another part of the user interface. The parent is the Grid, and we are trying to use TranslateTransform along with the MouseLeftButtonDown, MouseMove (etc.) events, as recommended in various online examples. For instance:

http://www.85turns.com/2008/08/13/drag-and-drop-silverlight-example/

We do this in IronPython, but this should be more or less inappropriate. The start of the drag and drop starts correctly, but the element that we are dragging appears in the "wrong" location (a shift of several hundred pixels to the right and down from the cursor), and I cannot understand why for life.

Base xaml:

<Grid x:Name="layout_root">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="120"/>
    </Grid.RowDefinitions>
    <Border x:Name="drag" Background="LightGray" Width="40" Height="15"
     Visibility="Collapsed" Canvas.ZIndex="10">
        <Border.RenderTransform>
            <TranslateTransform x:Name="transform" X="0" Y="0" />
        </Border.RenderTransform>            
        <TextBlock x:Name="dragText" TextAlignment="Center"
         Foreground="Gray" Text="foo" />
    </Border>
    ...
</Grid>

startDrag MouseLeftButtonDown ( TextBlock TreeViewItem.Header). onDrag MouseMove. self.root - Application.Current.RootVisual( UI app.xaml):

def startDrag(self, sender, event):
    self.root.drag.Visibility = Visibility.Visible
    self.root.dragText.Text = sender.Text
    position = event.GetPosition(self.root.drag.Parent)

    self.root.drag.transform.X = position.X
    self.root.drag.transform.Y = position.Y

    self.root.CaptureMouse()
    self._captured = True

def onDrag(self, sender, event):
    if self._captured:
        position = event.GetPosition(self.root.drag.Parent)
        self.root.drag.transform.X = position.X
        self.root.drag.transform.Y = position.Y

, . , ?

+3
1

, , Margin TranslateTransform:

def startDrag(self, sender, event):
    self.root.drag.Visibility = Visibility.Visible
    self.root.dragText.Text = sender.Text

    self.root.CaptureMouse()
    self._captured = True
    self.root.MouseLeftButtonUp += self.stopDrag
    self.root.MouseLeave += self.stopDrag
    self.onDrag(sender, event)

def onDrag(self, sender, event):
    if self._captured:
        position = event.GetPosition(self.root.layout_root)
        self.root.drag.Margin = Thickness(position.X, position.Y, 0, 0)
        self.root.drag.UpdateLayout() 
0

Source: https://habr.com/ru/post/1738083/


All Articles