|
| 1 | +--- |
| 2 | +title: Using the Drop Method in DragDropBehavior with CollectionView for .NET MAUI |
| 3 | +description: Learn how to notify the view model about drag-and-drop actions in CollectionView for .NET MAUI using the Drop method in DragDropBehavior. |
| 4 | +type: how-to |
| 5 | +page_title: Handling DragDropBehavior in CollectionView for .NET MAUI |
| 6 | +slug: collectionview-maui-dragdrop-notify-viewmodel |
| 7 | +tags: collectionview, .net maui, dragdropbehavior, drop-method, viewmodel |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +| Version | Product | Author | |
| 14 | +| --- | --- | ---- | |
| 15 | +| 10.1.0 | Telerik UI for .NET MAUI CollectionView |[Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 16 | + |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to notify the view model when drag-and-drop actions are performed in the [CollectionView for .NET MAUI](https://docs.telerik.com/devtools/maui/controls/collectionview/overview). Unlike the `RadListView.ReorderEnded` event, CollectionView uses `DragDropBehavior`, but it isn't clear how to inform the view model of the drop action. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to handle drag-and-drop in CollectionView for .NET MAUI? |
| 24 | +- How to use the `Drop` method of `DragDropBehavior` in CollectionView? |
| 25 | +- How to notify a view model of drag-and-drop actions in CollectionView for .NET MAUI? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To notify the view model of drag-and-drop actions in the CollectionView for .NET MAUI, use the `Drop` method in the `DragDropBehavior`. This method is triggered when an item is dropped. You can call this method within the context of the behavior to handle the drag-and-drop operation. |
| 30 | + |
| 31 | +### Example |
| 32 | +Here is an example of implementing the `Drop` method: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public class CustomDragDropBehavior : CollectionViewDragDropBehavior |
| 36 | +{ |
| 37 | + public override void Drop(CollectionViewDragDropContext state) |
| 38 | + { |
| 39 | + var collectionView = this.CollectionView; |
| 40 | + if (collectionView != null) |
| 41 | + { |
| 42 | + base.Drop(state); |
| 43 | + // Notify the view model about the drop action |
| 44 | + var viewModel = collectionView.BindingContext as YourViewModel; |
| 45 | + // add your logic here |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Key Points |
| 52 | + |
| 53 | +1. Override the `Drop` method in a custom behavior class. |
| 54 | +2. Access the `CollectionView`. |
| 55 | +3. Notify the view model by invoking a method or updating properties. |
| 56 | + |
| 57 | +For additional information on drag-and-drop functionality, refer to the [DragDrop Grouped Items documentation](https://docs.telerik.com/devtools/maui/controls/collectionview/drag-and-drop/dragdrop-grouped-items). |
| 58 | + |
| 59 | +## See Also |
| 60 | + |
| 61 | +- [CollectionView Drag-and-Drop Overview](https://docs.telerik.com/devtools/maui/controls/collectionview/drag-and-drop/overview) |
0 commit comments