Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1192
Drag and Drop frame work XamWebTree
posted

How to implement Drag and Drop Frame work with XamWebTree to drag and drop folder or XamWebTreeItem with in Tree. I am not using Drag-Drop facility of XamWebTree

Parents
  • 8831
    Suggested Answer
    posted

    Hi samireclp,

    When you want to deal with drag-and-drop in XamWebTree you should provide HierarchicalDataTemplate for the XamWebTree. In the sample below as data source for the XamWebTree is used instance of ObservableCollection<Category> collection where:

    public class Category

    {

        public string CategoryName { get; set; }

     

        public ObservableCollection<Movie> Movies { get; set; }

    }

     

    public class Movie

    {

        public string Name { get; set; }

     

        public int Rating { get; set; }

    }

    And that is the template you can use in this case:

    <Grid.Resources>           

    <ig:HierarchicalDataTemplate x:Key="hierarchyItemTemplate"                                             ItemsSource="{Binding Path=Movies}">

            <DataTemplate>

                <Grid Background="SteelBlue">

     

                    <ddm:DragDropManager.DragSource>

                        <ddm:DragSource IsDraggable="True"

                                        DragChannels="CategoryChannel"

                                        DataObjectBinding="{Binding}"

                                        DragStart="XamWebTreeDragStart"

                                        Drop="XamWebTreeDrop"/>

                    </ddm:DragDropManager.DragSource>

     

                    <ddm:DragDropManager.DropTarget>

                        <ddm:DropTarget IsDropTarget="True"                                                           DropChannels="CategoryChannel"/>

                    </ddm:DragDropManager.DropTarget>

     

                    <TextBlock Text="{Binding Path=CategoryName}" Margin="5"/>

     

                </Grid>

            </DataTemplate>

     

            <!-- 2nd level-->

            <ig:HierarchicalDataTemplate.HierarchicalItemTemplate>

                <ig:HierarchicalDataTemplate>

                    <DataTemplate>

                        <StackPanel Orientation="Horizontal">

     

                            <ddm:DragDropManager.DragSource>

                                <ddm:DragSource IsDraggable="True"

                                        DragChannels="CategoryChannel"

                                        DataObjectBinding="{Binding}"

                                        DragStart="XamWebTreeDragStart"

                                        Drop="XamWebTreeDrop"/>

                            </ddm:DragDropManager.DragSource>

     

                            <ddm:DragDropManager.DropTarget>

                                <ddm:DropTarget IsDropTarget="True"                                                           DropChannels="CategoryChannel"/>

                            </ddm:DragDropManager.DropTarget>

                           

                            <TextBlock Text="{Binding Path=Name}" />

                        </StackPanel>

                    </DataTemplate>

                </ig:HierarchicalDataTemplate>

            </ig:HierarchicalDataTemplate.HierarchicalItemTemplate>

        </ig:HierarchicalDataTemplate>

     

    </Grid.Resources>

     

    When you declare the template you can specify the UIElement that will be considered as source of the drag operation. That is accomplished as you set DragDropManager.DragSource attached property in given element. In this case for the first level that is the Grid element and for the second level is the StackPanel. You can specify which part can be treated as drop target as well. When you set the drop target element you should set DragDropManager.DropTarget attached property.

    Setting the drag-drop channels is optional and allows you to control where some dragged element can be dropped. Here the drag source and the drop target share the same channel.

    Note the usage of DataObjectBinding="{Binding}" on DragSource object. This will bound the drag source element with XamWebTreeItem.DataContext property and will propagate the data to DragDropEventArgs.Data property so you can use it in your event handlers. Here I set the event handlers just for DragStart and Drop events but there are also: DragCancel, DragEnd, DragEnter, DragLeave and DragOver events.

     

    And the XAML declaration for XamWebTree:

     

    <igTree:XamWebTree x:Name="xamWebTree"                                        HierarchicalItemTemplate="{StaticResource hierarchyItemTemplate}"

    ItemsSource="{Binding}">

     

    Here it is supposed that categories object is instance of ObservableCollection<Category>.

     

    [Code Behind]

     

    this.xamWebTree.DataContext = categories;

     

    I hope that will help.

    PPilev.

Reply Children