How do I implement drag drop in treeviews that are bound to multiple view models?

143 views
Skip to first unread message

Paqi

unread,
Feb 4, 2013, 3:56:11 PM2/4/13
to gong-wpf...@googlegroups.com
Hi,

I have a TreeView that is bound to multiple view models (different levels of the tree are bound to these models through the use of HierarchicalDataTemplates I list under TreeView.Resources). I found this model here:

I'd love to be able to implement this drag/drop pattern so that I can drag items across two different instances of this tree. I'm having trouble doing so though, because I can’t seem to add attachable properties to my HierarchicalDataTemplate definitions. 

Here's my XAML:

        <TreeView ItemsSource="{Binding Directories}"
                  dd:DragDrop.IsDragSource="True"
                  dd:DragDrop.IsDropTarget="True">
            <TreeView.ItemContainerStyle>
                <!-- bind TreeViewItem to TreeViewItemViewModel-->
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="FontWeight" Value="Normal" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.Resources>
                <HierarchicalDataTemplate
                    DataType="{x:Type local:DirectoryViewModel}"
                    ItemsSource="{Binding Children}"
                          <!-I'd like to set  dd:DragDrop.IsDragSource/Target to true here, but the dd namespace isn't recognized here--> 
                    >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate
                    DataType="{x:Type local:WorkspaceFileViewModel}"
                    ItemsSource="{Binding Children}"
                          <!-I'd like to set  dd:DragDrop.IsDragSource/Target to true here, but the dd namespace isn't recognized here--> 
                    >
                    <StackPanel Orientation="Horizontal">
                         <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate
                    DataType="{x:Type local:WorkspaceFileElementViewModel}"
                    ItemsSource="{Binding Children}"
                           <!-I'd like to set  dd:DragDrop.IsDragSource/Target to true here, but the dd namespace isn't recognized here--> 
                    >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Label}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>

robert....@gmail.com

unread,
Feb 5, 2013, 1:49:24 PM2/5/13
to gong-wpf...@googlegroups.com, robert....@gmail.com
So, this is a silly question that betrayed my lack of understanding about how attached properties work. That top portion:


<TreeView ItemsSource="{Binding Directories}"
                  dd:DragDrop.IsDragSource="True"
                  dd:DragDrop.IsDropTarget="True">

is all I needed. So long as every view model that's listed in the HierarchicalDataTemplates implements IsDragSource and IsDragTarget, those methods will be called at the appropriate time, because those sub-levels inherit the attached property. 

robert....@gmail.com

unread,
Feb 6, 2013, 9:39:08 AM2/6/13
to gong-wpf...@googlegroups.com, robert....@gmail.com
It appears this isn't enough. I thought it was working, but it turns out the default drop handlers where being used instead of the ones I defined in my various view models. This is because it's looking for a drag drop handler in the view model that's bound to the tree, and I had only defined the interface on the view models I listed in the HierarchicalDataTemplate. I believe my solution will involve using Multibinding and a MultiValueConverter. I'll post my solution when I come up with it.  
Reply all
Reply to author
Forward
0 new messages