Saad Abdullah
Saad Abdullah

Reputation: 2452

Drag drop file in WPF not working

I am using Mahapps for layout and AvalonDock for tabs and sub-windows layout. But unable to use drag drop functionality as the drag drop function never called. I have also set both(explorer and .exe) privileges to user according to link, but all in vain. Unable to get this thing work. Background of avalondock:DockingManager control is set to "#FF2D2D30". On dragging or dropping file from file explorer on dockingManager, nothing happens.

<avalonDock:DockingManager AllowDrop="True" x:Name="dockingManager" DockPanel.Dock="Right"  Theme="{Binding AvalonDockTheme}" PreviewDragEnter="DragFilesEntered" PreviewDrop="FilesDropped" PreviewDragOver="DragFilesEntered">
                    <avalonDock:LayoutRoot>

                        <avalonDock:LayoutPanel Orientation="Horizontal">
                            <avalonDock:LayoutDocumentPaneGroup>
                                <avalonDock:LayoutDocumentPane x:Name="layoutdoc_tabContent">

                                </avalonDock:LayoutDocumentPane>
                            </avalonDock:LayoutDocumentPaneGroup>
                        </avalonDock:LayoutPanel>
                    </avalonDock:LayoutRoot>
                </avalonDock:DockingManager>

Code Behind(C#) which i picked from some website

private void DragFilesEntered(object sender, DragEventArgs e)
        {
            MessageBox.Show("Hey");
            bool isValidFile = false;

            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                foreach (string filename in filenames)
                {
                    if (File.Exists(filename) == false)
                    {
                        isValidFile = false;
                        break;
                    }
                    FileInfo info = new FileInfo(filename);
                    if (!(info.Extension == ".bmp" || info.Extension == ".png" || info.Extension == ".jpg"))
                    {
                        isValidFile = false;
                        break;
                    }
                } 
            }
            if (isValidFile)
                e.Effects = DragDropEffects.Move;            
            else
                e.Effects = DragDropEffects.None;
        }

        private void FilesDropped(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach(var file in files)
                    ((MainViewModel)this.DataContext).CreateTab(layoutdoc_tabContent, file);
                e.Handled = true; 
            }
        }

EDIT:

<StackPanel PreviewDragEnter="DragFilesEntered" PreviewDrop="FilesDropped" PreviewDragOver="DragFilesEntered" AllowDrop="True" Width="100" Height="100" Orientation="Horizontal" Background="#FFDA1313">

                </StackPanel>

Still not working.Unable to call Drag function.

Upvotes: 3

Views: 2116

Answers (1)

rducom
rducom

Reputation: 7320

Try to put the AllowDrop="True" and the associated handlers directly on the LayoutDocumentPane.

In general, in XAML, you have to put theses on the overred FrameworkElement by the cursor when dropping.

Upvotes: 2

Related Questions