Hi, I need some help with WPF's drag and drop. I have a ListView list
composed of filenames from a selected directory and I would like to add the
drag and drop feature into my list so I can drag and drop the filenames into
a TreeView list using the left mouse button. The drag and drop I created
only works if I click and hold down my left mouse button, move the mouse
pointer off the list of filename, and then click and hold the right mouse
button while still holding down the left mouse button. In essence I need to
hold down both my mouse buttons to get it to work. Furthermore, if I click
and hold down my left mouse button to select an item from my filename list
and move the cursor over another item in the list, my selected item
changes to the items my mouse pointer is hovering over.
How can I fix the drag and drop in such a way that it allows me to select an
item from the filename list and use only the left mouse button to drag the
filename to my desired location? Also, is there a way to lock the selector
when I have my mouse button held down so my selected item don't change to
the new item my mouse pointer is hovering over?
Thanks
Here are the XAML and C# codes for the drag and drop:
****XAML****
<TreeView Name="tree" Header="tree" AllowDrop="True" Drop="tree_Drop"
DragEnter="tree_DragEnter"
>
...
</TreeView>
<ListView Name="files" MouseDown="list_MouseDown"
>
....
</ListView>
****C#****
ListView list ...
foreach( string s in Directory.GetFiles(filePat
h) )
{
list.items.add(s);
}
private void (object sender, MouseButtonEventArgs e)
{
ListView lv = (ListView)sender;
DragDrop.DoDragDrop(lv,
lv.SelectedItem.ToString()
,DragDropE
ffects.Cop
y);
}
private void tree_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(Dat
aFormats.T
ext))
e.Effects = DragDropEffects.Copy;
else
e.Effects = DragDropEffects.None;
}
private void tree_Drop(object sender, DragEventArgs e)
{
((TreeView)sender).Items.A
dd(e.Data.
GetData(Da
taFormats.
Text));
}
Start Free Trial