TreeViewItems Drag Drop with MVVM

2 posts, 0 answers
  1. Kal Sir
    Kal Sir avatar
    5 posts
    Member since:
    Apr 2010

    Posted 21 Jun 2011 Link to this post

    Hello,

    I am using MVVM to implement my Drag and Drop in my TreeView. I am using eventtrigger to bind my command when DragEnd event takes place. In Viewmodel, when the command calls my update method to call the appropriate service. But my issue is figuring out the previous and the new parent of the child/node/radtreviewitem that had been dragged and dropped. So that when i call my service I can tell what parent child is being deleted and which parent is getting a new child. I hope i am making sense here and your help ASAP is very much appreciated.

    I am attaching sample of my code. Hope this helps. Thank You very much.

    <telerik:RadTreeView   x:Name="tlrkTree"
                                           ItemsSource="{Binding TreeData, Mode=TwoWay}" BorderBrush="Gray" 
                                           BorderThickness="1" Grid.Row="2" 
                                           ItemContainerStyle="{StaticResource RadTreeViewStyle}" 
                                           Width="Auto" Height="400"  Margin="2,2,2,2"                                        
                                           IsDragDropEnabled="True" IsDragTooltipEnabled="True" 
                                           IsDropPreviewLineEnabled="True" 
                                           SelectedItem="{Binding SelectedTreeNode, Mode=TwoWay}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="DragEnded" >
                                <i:InvokeCommandAction Command="{Binding DragEndCommand}" CommandParameter="{Binding}"   />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <telerik:RadTreeView.ItemTemplate>
                            <telerik:HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                                <StackPanel>
                                    <TextBlock x:Name="textBlock">
                                         <Run Text="{Binding Name, Mode=TwoWay}"/>
                                    </TextBlock>
                                </StackPanel>
                            </telerik:HierarchicalDataTemplate>
                        </telerik:RadTreeView.ItemTemplate>
                    </telerik:RadTreeView>


    private readonly ITreeDataService _treeDataService ;
    private TreeNodeItemData  _selectedTreeNode;
    private DelegateCommand<object> _dragEndCommand;
      
            [ImportingConstructor]
            public TreeDataViewModel( ITreeDataService treeDataService)
            {
                _treeDataService = treeDataService;
                _dragEndCommand = new DelegateCommand<object>(DragEnded);
            }
      
           public ICommand DragEndCommand
            {
                get { return _dragEndCommand; }
            
      
            public TreeNodeItemData  SelectedTreeNode
            {
                get
                {
                    return _selectedTreeNode;
                }
                set
                {
                    _selectedTreeNode= value;
                }
      
            }
      
            public void DragEnded(object selectedItems)
            {
                //Previous Parent and the New Parent Node of the Selected Item - SelectedTreeNode
            }


    Thank You in advance,
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Jun 2011 Link to this post

    Hello Kal Sir,

    You may pass the DragEnd event arguments directly to the command and examine the TargetDropItem and DraggedItems properties. I am sending you a sample project illustrating the suggested approach. Please take a look at it and let us know if you need any further assistance.

    Kind regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top