简体   繁体   English

TranslateTransform用于在Silverlight中拖放

[英]TranslateTransform for drag and drop in Silverlight

We're trying to implement drag and drop in Silverlight (3). 我们正试图在Silverlight(3)中实现拖放。 We want users to be able to drag elements from a treeview onto another part of a UI. 我们希望用户能够将元素从树视图拖动到UI的另一部分。 The parent element is a Grid, and we've been trying to use a TranslateTransform along with the MouseLeftButtonDown, MouseMove (etc) events, as recommended by various online examples. 父元素是一个Grid,我们一直在尝试使用TranslateTransform以及MouseLeftButtonDown,MouseMove(等)事件,这是各种在线示例所推荐的。 For example: 例如:

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

We're doing this in IronPython, but that should be more or less irrelevant. 我们在IronPython中这样做,但这应该或多或少无关紧要。 The drag start is correctly initiated, but the item we are dragging appears in the 'wrong' location (offset a few hundred pixels to the right and down from the cursor) and I can't for the life of me work out why. 拖动开始是正确启动的,但是我们拖动的项目出现在“错误的”位置(从光标向右和向下偏移几百个像素)并且我不能为我的生活找出原因。

Basic xaml: 基本的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>

The startDrag method is triggered by the MouseLeftButtonDown event (on a TextBlock in a TreeViewItem.Header). startDrag方法由MouseLeftButtonDown事件触发(在TreeViewItem.Header中的TextBlock上)。 onDrag is triggered by MouseMove. onDrag由MouseMove触发。 In the following code self.root is Application.Current.RootVisual (top level UI element from app.xaml): 在下面的代码中,self.root是Application.Current.RootVisual(来自app.xaml的顶级UI元素):

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

The dragged item follows the mouse move, but is offset considerably. 拖动的项目跟随鼠标移动,但显着偏移。 Any idea what I am doing wrong and how to correct it? 知道我做错了什么以及如何纠正它?

So it turns out that I should have been setting the Margin instead of using TranslateTransform: 事实证明我应该设置保证金而不是使用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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM