我试图在3个树视图之间进行拖放项目。每个树源都由同一个类定义。
public class TreeItem : TreeViewItemBase
{
public int Id { get; set; }
public string Name { get; set; }
public string SubstringedName { get; set; }
public string Link { get; set; }
public bool IsChecked { get; set; }
public TreeItem Parent { get; set; }
public bool IsSingle { get; set; }
public List<TreeItem> TreeItems { get; set; }
}
但是我不知道如何在这3棵树之间传递数据…这是我现在拥有的:
private void tree_view_current_slides_Drop(object sender, DragEventArgs e)
{
MessageBox.Show("YEY");
}
private void tree_view_current_slides_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);
}
}
}
private void tree_view_current_slides_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void StartDrag(MouseEventArgs e)
{
_IsDragging = true;
object temp = this.tree_view_current_slides.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.tree_view_current_slides, data, dde);
}
_IsDragging = false;
}
我只是想知道如何将数据从一棵树获取到另一棵树,以及数据在哪里被丢弃
这是我给你的演示,涉及到两个treeview之间的拖放。
XAMl代码为:
<StackPanel>
<TreeView AllowDrop="True" Drop="OnDrop">
<local:DragableTreeViewItem Header="Employee1">
<local:DragableTreeViewItem Header="Jesper"/>
<local:DragableTreeViewItem Header="Aaberg"/>
<local:DragableTreeViewItem Header="12345"/>
</local:DragableTreeViewItem>
<local:DragableTreeViewItem Header="Employee2">
<local:DragableTreeViewItem Header="Dominik"/>
<local:DragableTreeViewItem Header="Paiha"/>
<local:DragableTreeViewItem Header="98765"/>
</local:DragableTreeViewItem>
</TreeView>
<TreeView AllowDrop="True" Drop="OnDrop">
<local:DragableTreeViewItem Header="Employee3">
<local:DragableTreeViewItem Header="Jesper3"/>
<local:DragableTreeViewItem Header="Aaberg3"/>
<local:DragableTreeViewItem Header="123453"/>
</local:DragableTreeViewItem>
<local:DragableTreeViewItem Header="Employee4">
<local:DragableTreeViewItem Header="Dominik4"/>
<local:DragableTreeViewItem Header="Paiha4"/>
<local:DragableTreeViewItem Header="987654"/>
</local:DragableTreeViewItem>
</TreeView>
</StackPanel>
它的cs代码是:
public void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetData("DragableTreeViewItem") != null)
{
var item = e.Data.GetData("DragableTreeViewItem") as DragableTreeViewItem;
DragableTreeViewItem newItem = new DragableTreeViewItem();
newItem.Header = item.Header;
foreach (var child in item.Items)
newItem.Items.Add(child);
}
}
DragableTreeViewItem.cs的代码为:
public class DragableTreeViewItem : TreeViewItem
{
public DragableTreeViewItem()
{
this.Drop += (sender, e) =>
{
if (e.Data.GetData("DragableTreeViewItem") != null)
((DragableTreeViewItem)e.Data.GetData("DragableTreeViewItem")).SetParent(sender);
};
}
public void SetParent(object newParent)
{
if (Parent is TreeView)
((TreeView)Parent).Items.Remove(this);
if (Parent is DragableTreeViewItem)
((DragableTreeViewItem)Parent).Items.Remove(this);
if (newParent is TreeView)
((TreeView)newParent).Items.Add(this);
if (newParent is DragableTreeViewItem)
((DragableTreeViewItem)newParent).Items.Add(this);
}
protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
DragDrop.DoDragDrop(this.Parent, new DataObject("DragableTreeViewItem", this, true), DragDropEffects.Move);
base.OnMouseMove(e);
}
}