Unable to Drag and Drop with WPF using a single mouse click (requires double mouse click)

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(filePath) )
{
    list.items.add(s);
}

private void (object sender, MouseButtonEventArgs e)
{
    ListView lv = (ListView)sender;
    DragDrop.DoDragDrop(lv,
lv.SelectedItem.ToString(),DragDropEffects.Copy);
}

private void tree_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.Text))
      e.Effects = DragDropEffects.Copy;
    else
      e.Effects = DragDropEffects.None;
}

private void tree_Drop(object sender, DragEventArgs e)
{
    ((TreeView)sender).Items.Add(e.Data.GetData(DataFormats.Text));
}
LVL 1
DrivenXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:

public Window1()
        {
            InitializeComponent();
            string []str = new string[]{"a","b","c","d","e"};
            listView1.ItemsSource = str;
 
            treeView1.AllowDrop = true;
            listView1.MouseDown += new MouseButtonEventHandler(listView1_MouseDown);
            listView1.DragOver += new DragEventHandler(listView1_DragOver);
            treeView1.Drop += new DragEventHandler(treeView1_Drop);
        }
 
        void treeView1_Drop(object sender, DragEventArgs e)
        {
            treeView1.Items.Add(e.Data.GetData(DataFormats.StringFormat));
        }
 
 
        void listView1_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }
 
        void listView1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (listView1.SelectedItem != null)
                {
                    DragDrop.DoDragDrop(listView1, listView1.SelectedItem, DragDropEffects.Copy);
                }
            }
        }

Open in new window

0
DrivenXAuthor Commented:
Thanks for the code snippet gauthampj, I still get the problem of needing to use both mouse click to get it to work.

Still have to hold down the left mouse and then click or hold down the right mouse to work.

However, it works if I select an item, move the mouse off the list and then hold down the left mouse
button.

Any ideas? sorry for the late reply
Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

DrivenXAuthor Commented:
We are not certain where the problem could be, if anyone would like more detail about the code or anything specific you have in mind to solving this problem, please let me know.  But I will try to start from scratch in building the list and incorporating drag and drop to see if I could ask a more specific question in the future.  But any suggestions are welcome, thanks.
0
DrivenXAuthor Commented:
Thanks again gauthampj! Will check out this link, sorry I wont be able to get a chance to work on this problem for the next couple of days, but I will try to get back to you asap.
0
DrivenXAuthor Commented:
thanks for the link
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.