I'm new to your DragAndDropManager class and have been reading up on it lately. I'm able to get the samples working without any problem and have been trying to adjust some of the sample code to meet a need I have. The need is this. I have a ListBox (A) with Bird objects within it and a ListBox (B) with Dinosaurs in it. Both list's items can be dragged within their same list to reorder them, however I want to prevent items from ListBox (A) being dropped onto ListBox (B). Now, I can determine the type of object in the destination list during the DropQuery event handler, but I can't seem to figure out how to prevent the OnDropInfo event from firing.
Additionally, I can prevent the Item from ListBox (A) from being added to ListBox (B) in the OnDropInfo event (based on some typeof() logic), however at this point the Item from ListBox (A) has already been removed from ListBox (A).
I would, ideally, like to be able to set the status of the DragDropQuery event args to DropImpossible when I try to drag an Item from a ListBox whose Items are of a different than the destination.
Does this make sense?
7 Answers, 1 is accepted
Hello Scott McEachern,
You can find a wide arrange of topics on Dran and Drop on our online documentation page. For that particular problem, you could use the OnDropQuery method and cancel the drop operation is the drop is invalid. For example:
the Telerik team
Thanks for the quick response. I have added the logic that checks the data type of the payload object against the data type of the objects in the destination list within the OnDropQuery event, however the OnDropInfo still fires. I have ensured that e.QueryResult get's set to false in this condition.
I guess I'm confused about the purpose of the Handled property and the QueryResult property on the DragDropQueryEventArgs object. What behavior is expected when their values change?
Setting the QueryResult property (in OnDropQuery) to false doesn't prevent the OnDropInfo method from firing when the user releases the mouse when trying to drop a Bird object (A) into ListBox of Dinosaurs (B).
Q: Is it necessary to place this same conditional logic within the OnDropInfo event to prevent the item from being added to the list? If that is true, then I would argue that it is pointless to check for this condition in the OnDropQuery method, per your suggestion.
I would like to know if it is possible to deny the Drop (and NOT remove the object from List (A)) during a failed Drop AND prevent the OnDropInfo event from firing when the object and its destination list object are not the same object. Does this make sense?
I know this is possible, but I'm just not sure how Telerik would have intended for it to happen.
Here's my XAML:
Here's my code. Its pretty straight forward.
In this event handler I can access the destination of the Drop (meaning, the place where the user intends to drop their object). I can then check to see if this destination object and the payload object are the same object and can then deny the drag / drop process from here. However, the DragDropEventArgs object doesn't contain a QueryResult object that can be set to "false" (I'm still not sure what this does anyway), and I can't explicitly set the DragStatus to DropImpossible as I would like to.
I know that I am probably going about this in a manner that wasn't intended, but the object structure and the pattern employed are confusing to me. The documentation doesn't offer much, either in the way of explaining the nature of the properties of the EventArgs objects.
Please let me know if what I am asking for is prohibited and if there is a proper way of going about this.
DragCancel - the source is notified that the DragDrop has been cancelled. For example, the user may press Esc while dragging to cancel the event or the object may be released over a target that does not accept it.
I should add that I'd like to know I can have the target list "not accept" an object of another type. I think this might be the best approach. I see no documentation explaining how or when this two-way handshake process occurrs.
I have checked the code sent and found that indeed the drop operation is executed when dragging between the two listboxes. The reason for that is the code in the DropQuery - it check for payload type only when destination is Listbox, but not when destination is Listboxitem. Adding the code in this case result in correct behavior.
About the other questions I will try to summarize the drag drop mechanism in few words:
- dragquery and dropquery are raised before draginfo and dropinfo to check whether the current operation is allowed or not. This is done by using QueryResult. If the result is false the corresponding info method is not called.
- The progress of the drag operations can be tracked through Options.Status property. It cannot be changed from outside, since it reflects the final result from the mouse position, state and query results in the drag/drop query methods. As an example, I have added status textblock that displays the dragstatus in DragInfo method. it will display DropPossible,DropImpossible,DragComplete,DragCancel depending on current drag state.
More information about the events and statuses can be found in these help articles:
Also, you can check the attached project using the code posted earlier in the thread.
Let us know if you have any further questions.
the Telerik team
That adjustment made a big difference. I didn't realize that I had to check for both ListBox and ListBoxItems. It seems as though checking for a ListBox only should be sufficient, but now it seems that ListBoxItems are almost always found to be the drop destination in this example. In what cases would the ListBox be found? When there are no items in the list?
Thanks again, this is working now.
I am glad that all is fine now. You are correct, the ListBox will be found when you are dropping on empty area.
the Telerik team