1

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

我有一个带有分层数据模板的树视图,如此处所示;

<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 获取我的视图模型并将它们拖过,但是我需要找到我将其拖入的新类别视图模型,但是如何获取放置目标数据?可能是发件人或args中的某些东西?

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

资产概览

4

2 回答 2

2

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

于 2013-06-27T13:17:22.473 回答
-1

条件是:

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

对于 MouseButtonState.Pressed 和 OR 对于其他两个条件!

于 2016-04-08T06:39:31.310 回答