A Blazor component that supports drag and drop of elements. Tested on Web Assembly.
- Nesting Mutliple Lists.
- Support of Dragging Data from other components.
- Polymorphism Support.
Continue reading to discover more features.
Install the Package using PM :
Install-Package WK.Blazor.DragDrop -Version 1.0.0
In the Program.cs Add the following namespace:
using DragDrop.Blazor;
In the Program.cs Add the following line in the main function:
builder.Services.AddSingleton<DragDropService>();
@using DragDrop.Blazor
<DragDropList></DragDropList>
- TItem (Type Parameter): The Type of the Items in the List.
- Items (IList<TItem): The List of Items. Default value is of Type : List.
- Capacity (integer): The number of items the list can hold. Evaluated on a Drop Event only. Modifying the List from outside the component isn't checked. Default value is : int.MaxValue.
- OnCapacityReachedRejection (Event Callback): Event Callback with the rejected item as argument.
- OnItemAdded (Event Callback): Event Callback with last added item as argument. Modifying the List from outside the component isn't checked.
- DefaultDropMode (DefaultDrop enum): DefaultDrop.Start : When an Item is Dropped on the List Itself, it is added on the top of the List. DefaultDrop.End : When an Item is Dropped on the List Itself, it is added at the end of the List. Default value is DefaultDrop.End .
- AllowsDrag (bool): A boolean that allows dragging of the Elements with a default value of true. Drag Items inside of the list to swap positions. Drag Items Outside of the list to perform a cut/copy.
- Copy (Func<TItem, TItem>): A Function that tells the component how to copy items if they're dragged. When not set, Items dragged outside the list will be Cut. Parameter : Dragged Item. Return Value : Copy of Dragged Item.
- ItemTemplate (RenderFragment): A render fragment to display each item.
- ItemTemplateClass (string): The CSS class of the ItemTemplate.
- EmptyView (RenderFragment): A render fragment to display when the list is empty.
- Verify (Func<TItem, bool>): A function to add extra verification before dropping an Item. When Verify returns True, the Item will be Added.
- OnRejected (Event Callback): An Event Callback with the Item (that didn't pass the Verify test) as Argument.
- OnItemSwap (Event Callback<Tuple<int,int>>): An Event Callback with a Tuple : Item a was swapped with an item b. So a has moved from the index tuple.FirstItem to tuple.SecondItem.
You can add other html attributes, and of you course you can change the DragDropList Css.
@using DragDrop.Blazor
@inject DragDropService DD
- SetData(object o): Sets the dragged data(if you wish to drag data from outside of the list).
- Clear(): Clears the dragged data. In case you needed it.
Samples Project can be found here. It is advised to take a look at them before starting.
You can do the following
<div draggable="true" @ondragstart="()=>DD.SetData(new Object())"></div>