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

TreeViewItems Drag Drop with MVVM

1 Answer 70 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kal Sir
Top achievements
Rank 1
Kal Sir asked on 21 Jun 2011, 08:20 PM
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,

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Jun 2011, 01:37 PM
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
Tags
General Discussions
Asked by
Kal Sir
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or