3

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.

Community
  • 1
  • 1
Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42

1 Answers1

2

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.

rducom
  • 7,072
  • 1
  • 25
  • 39
  • LayoutDocumentPane doesnot have AllowDrop property :( – Saad Abdullah Feb 14 '15 at 19:56
  • So put it on the content of `LayoutDocumentPane` (the thing inside). (a Grid or something) – rducom Feb 14 '15 at 19:57
  • Yeah! but whats the purpose of that? because i am dynamically creating a stackpanel and adding that into LayoutDocumentPane content(in children) to create tabs of the images dropped. The user should drop image file on docking area not on the image tab already created. – Saad Abdullah Feb 14 '15 at 20:01
  • Internally, the DockingManager when empty exposes a control on which is set the background color etc. The problem is this control is defined deep in the DockingManager Style. So you can't plug directly on it, except by redefining it's style manually by extracting it with Blend (which is too boring). So the way to go is to expose a control yourself, which handles the D'n D logic. – rducom Feb 14 '15 at 20:05
  • Please check the edited question. I have tried the same thing with stackpanel but drag drop function never called – Saad Abdullah Feb 14 '15 at 20:11
  • Thanks @Sharped..Managed to solve the problem by executing the application from bin folder and assigning the background of avalondockingManager to some color. Thanks for the help btw :) – Saad Abdullah Feb 14 '15 at 20:19