WP7的任意拖放功能

5
我正在尝试找到一种显示文本块的方法,或者允许我任意拖放控件在屏幕上移动的方法。 我已经搜遍了谷歌和这里,但是我发现每一个与拖放有关的问题都是关于交换数据而不仅仅是位置。 有没有人知道准备好的东西,或者您可以指导我应该寻找什么方向?
3个回答

4
您可以通过使用行为来实现此目的:
<TextBlock Text="Hello!">
    <i:Interaction.Behaviors>
        <el:MouseDragElementBehavior ConstrainToParentBounds="True"/>
    </i:Interaction.Behaviors>
</TextBlock>

您需要在解决方案中添加对 Microsoft.Expression.Interactions 的引用,并在 XAML 文件的顶部添加以下命名空间:

xmlns:el="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

谢谢!这是目前为止最简单的选项。或者,你如何能够将此行为添加到动态创建的控件中? - Lucas
1
假设您有一个名为Label的元素:var behavior = new MouseDragElementBehavior(); behavior.Attach(this.Label); - Kevin Gosse

3
代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Height="30" Margin="125,132,0,0"
               Name="textBlock1" Text="TextBlock"
               Width="83" MouseMove="textBlock1_MouseMove" />
</Grid>

以及背后的代码:

private void textBlock1_MouseMove(object sender, MouseEventArgs e)
{
   TextBlock realSender = (TextBlock)sender;
   var theParent = (Grid)realSender.Parent;
   var position = e.GetPosition(theParent);
   realSender.Margin = new Thickness(
                       position.X - realSender.Width / 2,
                       position.Y - realSender.Height / 2, 0, 0);

}

正是我想要的!你能再解释一下为什么要减去realSender.Width/2并除以2吗?另外,在设置了宽度和高度的情况下,Margin值中是否真的需要设置RIGHT和BOTTOM的值? - besworland

0

工具包示例曾经包括了如何完成这个例子。

不确定它是否仍然存在,因为它是基于手势支持的,而手势支持此后已被弃用。如果消失了,请查看2011年8月版本


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接