gpt4 book ai didi

wpf - 在 TreeView 中拖放

转载 作者:行者123 更新时间:2023-12-02 06:11:41 53 4
gpt4 key购买 nike

我正在尝试在 TreeView 中拖放文件,但我不知道如果我运行它并尝试拖动文件,它会崩溃。

下面的代码是我尝试过的。请帮忙。

private void TreeViewItem_Drop( object sender, DragEventArgs e)
{
TreeViewItem treeViewItem = e.Source as TreeViewItem;
TreeViewItem obj = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;

if ((obj.Parent as TreeViewItem) != null)
{
(obj.Parent as TreeViewItem).Items.Remove(obj);
}
else
{
treeViewItem.Items.Remove(obj);
treeViewItem.Items.Insert(0, obj);
e.Handled = true;
}
}

private void TreeViewItem_MouseLeftButtonDown( object sender,MouseButtonEventArgs e)
{
DependencyObject dependencyObject = _treeview.InputHitTest(e.GetPosition(_treeview)) as DependencyObject;

Debug.Write(e.Source.GetType().ToString());

if (dependencyObject is TextBlock)
{
TreeViewItem treeviewItem = e.Source as TreeViewItem;

DragDrop.DoDragDrop(_treeview, _treeview.SelectedValue, DragDropEffects.Move);
e.Handled = true;
}
}

最佳答案

这篇文章非常有帮助。 Drag drop wpf

此代码可能对您也有用。

Point _startPoint;
bool _IsDragging = false;

void TemplateTreeView_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed ||
e.RightButton == MouseButtonState.Pressed && !_IsDragging)
{
Point position = e.GetPosition(null);
if (Math.Abs(position.X - _startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(position.Y - _startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance)
{
StartDrag(e);
}
}
}

void TemplateTreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}

private void StartDrag(MouseEventArgs e)
{
_IsDragging = true;
object temp = this.TemplateTreeView.SelectedItem;
DataObject data = null;

data = new DataObject("inadt", temp);

if (data != null)
{
DragDropEffects dde = DragDropEffects.Move;
if (e.RightButton == MouseButtonState.Pressed)
{
dde = DragDropEffects.All;
}
DragDropEffects de = DragDrop.DoDragDrop(this.TemplateTreeView, data, dde);
}
_IsDragging = false;
}

关于wpf - 在 TreeView 中拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1026179/

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