This is a migrated thread and some comments may be shown as answers.

Drag and drop user controls in main page

1 Answer 78 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Melvin
Top achievements
Rank 1
Melvin asked on 23 Feb 2012, 03:53 AM
Hi,

I have 3 user controls hosted in a main page. I would like to make the user controls draggable within the main page itself. How do I implement them? Currently, this is wat I did:

  <ListBox x:Name="allApplicationsBox" BorderThickness="0" telerik:StyleManager.Theme="Metro"
     AllowDrop="True" Background="Transparent" BorderBrush="Transparent"
     ItemContainerStyle="{StaticResource draggableItemStyle}">
                <ListBox.Items>
                    <ListBoxItem>
                        <my:TopContainer x:Name="topContainer" Grid.Row="0" HorizontalAlignment="Left" Width="1190" telerik:DragDropManager.AllowDrag="True"/>
                    </ListBoxItem>
                    <ListBoxItem>
                        <my:MiddleContainer x:Name="middleContainer" Grid.Row="1" HorizontalAlignment="Left" Width="1190" telerik:DragDropManager.AllowDrag="True"/>
                    </ListBoxItem>
                    <ListBoxItem>
                        <my:BottomContainer x:Name="bottomContainer" Grid.Row="2" HorizontalAlignment="Left" Width="1190" telerik:DragDropManager.AllowDrag="True"/>
                    </ListBoxItem>
                </ListBox.Items>
            </ListBox>

 

The styles are as follows:

            <Style TargetType="ListBoxItem" x:Key="draggableItemStyle">
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Foreground" Value="#000000" />
                <Setter Property="telerik:DragDropManager.AllowDrag" Value="True" />
                <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
            </Style>

Need some help and guidance on how to make it done, preferrably with some code snippets.

Thanks and regards,
ML

1 Answer, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 27 Feb 2012, 01:03 PM
Hello Melvin,

I am not sure I understand the problem. Are you trying to implement some kind of a "designer" like Visual Studio's designer, or just to place the controls from the ListBox on in predefined places on the page? 

Either way, there are various ways to accomplish this. In general, you can use the DragDropManager events to accomplish the desired functionality. You can use the DragInitializing handler to set the dragged data to the control that you are dragging.

private void OnDragInitialize(object sender, DragInitializeEventArgs args)
        {
            args.AllowedEffects = DragDropEffects.All;
            args.Data = ((FrameworkElement)args.OriginalSource).ChildrenOfType<TopContainer>().FirstOrDefault(); // This should be the control you are trying to drag
        }
You can place the control you dragged in the  Drop event handler. 
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs args)
{
     (sender as FrameworkElement).Children.Add(args.Data as FrameworkElement); // Add the control you are dragging in the children collection of the target. If needed you can adjust some manditory properties before adding it. Like GridRow, CanvasTop etc.
}

You can use the other event to adjust the other properties you need to implement the functionality.

Hope this helps!

Greetings,
Nik
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
DragAndDrop
Asked by
Melvin
Top achievements
Rank 1
Answers by
Nick
Telerik team
Share this question
or