你如何得到拖放下降目标对象?

时间:2013-06-27 12:44:03

标签: c# wpf mvvm treeview

我希望标题不会太混乱,不完全确定如何描述它。反正!

我有一个使用hierarchicalDataTemplates的树视图,如下所示;

<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>

我已经在类别标题上将AllowDrop设置为true,并且我已经设置了一些处理以在两者之间拖动资产。这是我的拖拽代码;

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);
        } 
    }

然后放弃;

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;
        }
    }

这很好用,我可以从dragEventArgs获取我的viewmodel并将它们拖过来,但是我需要找到我正在拖动它的新类别viewmodel,但是你如何获得drop target数据呢?它可能是发件人或args中的东西吗?

编辑:为了清楚起见,这是我的观点图片。拖动源将是子(笔记本电脑),放置目标将是类别(硬件等)。

asset overview

2 个答案:

答案 0 :(得分:2)

在StackPanel_Drop方法中,e.OriginalSource对象应该是放置目标。您应该能够从中获得所需的信息。

答案 1 :(得分:-1)

条件是:

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

AND用于MouseButtonState.Pressed,OR用于其他两个条件!