Noorul
Noorul

Reputation: 923

Drag and drop on dynamically created controls in windows phone

I need to drag and drop controls like TextBox and Image.

I got a solution t do this,

here the code for drag and drop using interactions and mouse element behaviour

 <TextBox Height="30" Margin="70,50,250,133"
           Name="textBlock1" Text="Pavan Pareta" MouseMove="MouseMoving">
            <i:Interaction.Behaviors>
                <el:MouseDragElementBehavior ConstrainToParentBounds="True"/>
            </i:Interaction.Behaviors>
        </TextBox>

        <Image Height="110" Name="image1" Stretch="Fill" Source="/WmDev_DragAndDrop;component/Images/house.png" Margin="254,90,95,407" MouseMove="MouseMoving">
            <i:Interaction.Behaviors>
                <el:MouseDragElementBehavior ConstrainToParentBounds="True"/>
            </i:Interaction.Behaviors>
        </Image>

and the MouseMoving event is

private void MouseMoving(object sender, MouseEventArgs e)
{
        if (sender.GetType() == typeof(TextBox))
        {
            TextBox realSender = (TextBox)sender;
            Canvas.SetZIndex(realSender, idx++);
        }
        else if (sender.GetType() == typeof(Image))
        {
            Image realSender = (Image)sender;
            Canvas.SetZIndex(realSender, idx++);
        }
}

But i need to implement this on dynamically created controls. I dont find way to do this on pragmatically created runtime controls. Can anybody help me to do so.

Thank you.

Upvotes: 0

Views: 681

Answers (1)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

If the question is how to add a behavior to a control instanciated from code behind, here is how you can do that:

 Image image1=new Image();
 ...
 MouseDragElementBehavior mouseDragElementBehavior=new MouseDragElementBehavior();
 mouseDragElementBehavior.ConstrainToParentBounds=true;
 Interaction.GetBehaviors(img).Add(mouseDragElementBehavior);

Upvotes: 2

Related Questions