gpt4 book ai didi

c# - 如何获得拖放目标对象?

转载 作者:行者123 更新时间:2023-12-03 10:38:01 25 4
gpt4 key购买 nike

我希望标题不太困惑,不确定如何描述它。反正!

我有一个带有分级数据模板的树 View ,如下所示;

<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,但是如何获取放置目标数据?可能是发件人或args中有东西吗?

编辑:为清楚起见,这是我的看法。拖动源将是 child (笔记本电脑),放置目标将是类别(硬件等)。

最佳答案

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

关于c# - 如何获得拖放目标对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17343531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com