How to Drag and drop in m:ui?

Feb 17, 2014 at 12:38 PM
Hello,

I want to enable in moder ui the drag and drop. I have tried in my view to add the following:
    <ListBox ItemsSource="{Binding Files}" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AllowDrop="True" Drop="ListBox_Drop">
It does not work. Am I missing something?
How do you achieve drag and drop in m:ui?
Feb 28, 2014 at 4:18 AM
You may have already solved your problem, but this is what I have done.

Add the following attributes
AllowDrop="True" Drop="DropBox_Drop" DragOver="DropBox_DragOver" DragLeave="DropBox_DragLeave"
to the ListBox and use the following code
public ObservableCollection<string> Files
{
            get
            {
                return _files;
            }
 }
private ObservableCollection<string> _files = new ObservableCollection<string>();

private void DropBox_Drop(object sender, DragEventArgs e)
 {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //  _files.Clear();
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string filePath in files)
                {
                    _files.Add(filePath);
                }

            }
            var listbox = sender as ListBox;
            listbox.Background = Brushes.LightSlateGray;         
}
private void DropBox_DragOver(object sender, DragEventArgs e)
{
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Copy;
                var listbox = sender as ListBox;
                listbox.Background = Brushes.RoyalBlue;            
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
}
private void DropBox_DragLeave(object sender, DragEventArgs e)
{
            var listbox = sender as ListBox;
            listbox.Background = Brushes.RoyalBlue;          
}

private void btnRemoveItem_Click(object sender, RoutedEventArgs e)
{
            while (DropBox.SelectedItems.Count > 0)
            {
                Files.RemoveAt(DropBox.SelectedIndex);
            }
}
Also, add a button which uses btnRemoveItem_Click to remove an item from the ListBox .
Feb 28, 2014 at 7:37 AM
Thanks for the response.
In fact my code was alright, it was a thing that I run my Visual Studio in Administrator mode and only the same privileged programs can drag and drop from each other.
So my solution was to open another application in administrator mode and it worked.