问题描述
如果这是不可能的,我也可以用2的TreeView控件。我是不会有在第二TreeView控件的层次结构。它会表现得像某种资源库。
If it's not possible, I can also use 2 TreeView controls. I just won't have a hierarchy in the second TreeView control. It's gonna act like some sort of repository.
任意code样品或教程将是非常有益的。
Any code sample or tutorial would be very helpful.
推荐答案
的ListView
不支持拖动和自然下降,但你可以用小一点使它的code:
ListView
does not support drag-and-drop naturally, but you can enable it with a small bit of code:
http://support.microsoft.com/kb/822483
下面是一个例子,专门做拖和下降从的ListView
到的TreeView
(这是一个专家性别更改链接,所以只需等待几秒钟,然后滚动至底部,在那里你会找到答案):
Here's an example that specifically does drag-and-drop from a ListView
to a TreeView
(it's an Expert Sex Change link, so just wait a few seconds and then scroll to the bottom, where you'll find the answers):
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_22675010.html
更新:
Update: Code from the link:
OK, create a listview and a treeview.
In my example, the listview is called listView1 and the treeview is called tvMain.
On the treeview, set AllowDrop to true.
Create an ItemDrag event on the listview:
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Copy);
}
In this example items from the listview are copied to the 'drop' object.
Now, create a DragEnter event on the treeview:
private void tvMain_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
This was easy. Now the hard part starts. The following code adds the selected (and dragged) listview items to an existing node (make sure you have at least one node already in your treeview or the example will fail!)
Create a DragDrop event on the treeview:
private void tvMain_DragDrop(object sender, DragEventArgs e)
{
TreeNode n;
if (e.Data.GetDataPresent("System.Windows.Forms.ListView+SelectedListViewItemCollection", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode dn = ((TreeView)sender).GetNodeAt(pt);
ListView.SelectedListViewItemCollection lvi = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
foreach (ListViewItem item in lvi)
{
n = new TreeNode(item.Text);
n.Tag = item;
dn.Nodes.Add((TreeNode)n.Clone());
dn.Expand();
n.Remove();
}
}
}
To change the cursor while dragging, you have to create a GiveFeedback event for the ListView control:
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
if (e.Effect == DragDropEffects.Copy)
{
Cursor.Current = new Cursor(@"myfile.ico");
}
}
myfile.ico should be in the same directory as the .exe file.
This is just a simple example. You can extend it any way you like.
I hope this helps.
这篇关于是否有可能有拖放从一个ListView到的WinForms一个TreeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!