Search code examples
wpfvb.netcomboboxdrag-and-drop

Drag and drop on editable combobox


In WPF, I need to drop a file to a combobox which is editable. I can drop on the arrow and on the border of the combobox, but not directly on the main white part (the textbox part) I have tried to add handler for the sub-textbox, but no luck. Here is the code I use for that (VB .net, I know...)

  CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).AllowDrop = True
  AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).Drop, AddressOf Combo_Fichier1_Drop
  AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).DragEnter, AddressOf Function_DragEnter
  AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).DragOver, AddressOf Function_DragEnter

My DragEnter function change the drag and drop effect to copy (working on the border and on the arrow) and my drop function simply set the text of the combobox with the name and path of the file. It's a sort of file selector with memory.


Solution

  • The problem likely occurs because the TextBox control supports the drag and drop of strings out of the box. Because of that it ignores other objects. It is possible to use the PreviewDragOver event to avoid this problem as described in this thread: https://stackoverflow.com/a/23150582/12797700