locked
Drag and Drop Events not fired RRS feed

  • Question

  • Has anyone been able to get Drag & Drop events (e.g dragging a file from IE) triggered in a Wpf Control embedded in ElementHost ? A fragment of my app set up is shown. Only the Main Form's drag/drop events get raised. Thanks.

     

    public partial class Form1 : Form

    {

     

    public Form1()

    {

    InitializeComponent();

     

    this.AllowDrop = true;

     

    this.DragEnter += new DragEventHandler(Form1_DragEnter);

     

    this.DragDrop += new DragEventHandler(Form1_DragDrop);

     

    //

     

    this.elementHost1.AllowDrop = true;

    ((

    Control)this.elementHost1).DragEnter += new DragEventHandler(OnElemHostDragEnter);

    ((

    Control)this.elementHost1).DragDrop += new DragEventHandler(OnElemHostDragDrop);

     

    //

     

    this.elementHost1.Child.AllowDrop = true;

     

    this.elementHost1.Child.DragEnter += new System.Windows.DragEventHandler(OnWpfDragEnter);

     

    this.elementHost1.Child.Drop += new System.Windows.DragEventHandler(OnWpfDrop);

    }

     

    void Form1_DragEnter(object sender, DragEventArgs e)

    {

     

    // If file is dragged, show cursor "Drop allowed"

     

    if (e.Data.GetDataPresent(DataFormats.FileDrop))

    e.Effect =

    DragDropEffects.All;

     

    else

    e.Effect =

    DragDropEffects.None;

    }

     

    Tuesday, November 9, 2010 5:53 PM

Answers

  • Hi Akin,

    I tested a sample, the events of the inner WPF element can be raised, what is the Chile element of the ElementHost, is it a Panel element? If it is a Panel, please set the Background property to Transparent, please refer to the MSDN document about this property: http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.background.aspx

     Remarks
    Panel elements do not receive mouse or stylus events if a Background is not defined. If you need to handle mouse or stylus events but do not want a background for your Panel, use Transparent.

     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
    
       Grid grid = new Grid()
       {
        Background = System.Windows.Media.Brushes.Transparent,
        AllowDrop = true
       };
       grid.DragOver += (o, e) => { Console.WriteLine("DragOver"); };
       grid.DragEnter += (o, e) => { Console.WriteLine("DragEnter"); };
       grid.DragLeave += (o, e) => { Console.WriteLine("DragLeave"); };
       elementHost1.Child = grid;
    
       this.AllowDrop = true;
       this.DragEnter += (o, e) => { Console.WriteLine("Winform DragEnter"); };
       this.DragDrop += (o, e) => { Console.WriteLine("Winform DragDrop"); };
      }
     }
    
    Sincerely,
    Bob Bao

     

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by Jie Bao Monday, November 22, 2010 4:45 AM
    Wednesday, November 10, 2010 12:35 PM