Lucas
Lucas

Reputation: 653

Arbitrary Drag and Drop for 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?

Upvotes: 5

Views: 979

Answers (3)

Kevin Gosse
Kevin Gosse

Reputation: 39047

You can do this by using behaviors:

<TextBlock Text="Hello!">
    <i:Interaction.Behaviors>
        <el:MouseDragElementBehavior ConstrainToParentBounds="True"/>
    </i:Interaction.Behaviors>
</TextBlock>

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

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

Upvotes: 4

Teemu Tapanila
Teemu Tapanila

Reputation: 1275

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

and the code behind:

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

}

Upvotes: 3

Matt Lacey
Matt Lacey

Reputation: 65586

The 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.

Upvotes: 0

Related Questions