Arrastrar y soltar arbitrarios para WP7

I'm trying to find a method of displaying a text block or that will allow me to arbitrarily drag and drop drop that control around the screen.

I've scoured google and here, but every drag and drop related question I find is around exchanging data, not just position.

Is anyone aware of something ready to go, or can you point me in the direction I should be looking?

preguntado el 31 de enero de 12 a las 08:01

3 Respuestas

You can do this by using behaviors:

<TextBlock Text="Hello!">
        <el:MouseDragElementBehavior ConstrainToParentBounds="True"/>

You need to add a reference to Microsoft.Expression.Interactions in your solution, and the following namespace at the top of your XAML file:


Respondido 11 Feb 12, 01:02

Thanks! This is by far the easiest option. Alternatively, how would you be able to add this behavior to controls dynamically created? - Lucas

Let's say you have an element called Label: var behavior = new MouseDragElementBehavior(); behavior.Attach(this.Label); - Kevin Gosse

El xaml:

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

y el código detrás:

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


Respondido 07 Feb 12, 04:02

Exactly what I was looking for! Could you please explain me additionally why do you subtract realSender.Width/2 and why it is divided on 2? Also in Margin value is it really necessary to set RIGHT and BOTTOM values if Width and Height are set? - besworland

La toolkit sample used to include an example of doing this.

Not sure if it's still in there though as it was based on the gesture support which has since been deprecated. If it's gone check the August 2011 version.

Respondido el 31 de enero de 12 a las 18:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.