Kestami
Kestami

Reputation: 2073

How do you get the drag drop drop target object?

I'm hoping the title wasn't too confusing, wasn't entirely sure how to describe it. Anyway!

I have a treeview with hierarchicalDataTemplates, as shown here;

<TreeView.Resources>

               <HierarchicalDataTemplate DataType="{x:Type WPFFM:AssetCategoryViewModel}" ItemsSource="{Binding Path= Children}" >
                <StackPanel Orientation="Horizontal" AllowDrop="True" Drop="StackPanel_Drop" DragEnter="StackPanel_DragEnter">
                 <TextBlock Text= "{Binding Description}" ContextMenu="{StaticResource assetOverviewContextMenu}"/>
                </StackPanel>
               </HierarchicalDataTemplate>

               <HierarchicalDataTemplate DataType="{x:Type WPFFM:AssetViewModel}" ItemsSource="{Binding Children}" >
                <StackPanel Orientation="Horizontal">
                 <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch"  ContextMenu="{StaticResource assetContextMenu}"
                                                    PreviewMouseLeftButtonDown="Asset_PreviewMouseLeftButtonDown" PreviewMouseMove="Asset_PreviewMouseMove"    />
                </StackPanel>
               </HierarchicalDataTemplate>

</TreeView.Resources>

I've set AllowDrop to true on the category headers, and i've set some handling up for dragging assets between the two. Here's my code for Dragging;

private void Asset_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(null);
    }

    private void Asset_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        // Get the current mouse position
        Point mousePos = e.GetPosition(null);
        Vector diff = startPoint - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed &&
            Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
        {


            TextBlock parentMenu = sender as TextBlock;
            AssetViewModel assetViewModelToSend = ((AssetViewModel)parentMenu.DataContext);



            // Initialize the drag & drop operation
            DataObject dragData = new DataObject("myFormat", assetViewModelToSend);
            DragDrop.DoDragDrop(parentMenu, dragData, DragDropEffects.Move);
        } 
    }

And dropping;

private void StackPanel_Drop(object sender, DragEventArgs e)
    {
        //TextBlock parentMenu = e as TextBlock;
        //AssetCategoryViewModel assetCat = ((AssetCategoryViewModel)parentMenu.DataContext);

        //MessageBox.Show(parentMenu.Text);


        if (e.Data.GetDataPresent("myFormat"))
        {
            AssetViewModel modelBeingSent = e.Data.GetData("myFormat") as AssetViewModel;
            MessageBox.Show(modelBeingSent.Description);

        }
    }

    private void StackPanel_DragEnter(object sender, DragEventArgs e)
    {
        if (!e.Data.GetDataPresent("myFormat") ||
    sender == e.Source)
        {
            e.Effects = DragDropEffects.None;
        }
    }

This works fine and i can get my viewmodel from the dragEventArgs and drag them across, however i need to find the new category viewmodel that i'm dragging it into, but how do you get the drop targets data? Is it perhaps something in the sender or args?

edit: For clarity here's a picture of my view. The drag source would be the child ( The laptop), and the drop target would be the category (hardware etc).

asset overview

Upvotes: 1

Views: 5292

Answers (2)

Feneck91
Feneck91

Reputation: 1

Condition is:

if ((e.LeftButton == MouseButtonState.Pressed && (
        Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))

AND for MouseButtonState.Pressed and OR for other both conditions !

Upvotes: -1

John Kraft
John Kraft

Reputation: 6840

In the StackPanel_Drop method, the e.OriginalSource object should be the drop target. You should be able to get the information you need from that.

Upvotes: 2

Related Questions