I would like to use drag and drop function in my application where i need to drag items from one listbox to another listbox.
I am using GongSolution.WPF.DragDrop.dll; assembly version 2.1.0
from [enter link description here][1] .
For checking to use the library, i have added two Listboxs with LT1, LT2.
<ListBox Name="LT1"
ItemTemplate="{StaticResource itemlisttemplate}"
Grid.Column="0"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
SelectionMode="Single"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<ListBox Name="LT2"
ItemTemplate="{StaticResource itemlisttemplate}"
Grid.Column="1"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
SelectionMode="Single"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
And a sample code to add few items into Listbox1 (LT1) so i can try to drag and drop the same to Listbox2 (LT2)
System.Object[] ItemObject = new System.Object[10];
for (int i = 0; i <= 3; i++)
{
ItemObject[i] = "Item" + i;
}
LT1.Items.AddRange(ItemObject);
On start of application, i can see two items are added into listbox1. but when i tried to copy items from listbox1 to listbox2, instead of drag, it is getting copied and moved to listbox2.
Request someone to guide/ suggest on what could be the problem here.
Or suggest me on any library for drag and drop function.
Thanks a lot.
As i was using listbox collections. My view was not getting updated on collection change hence, Listbox1 is still showing the items which are been dragged though it is already dragged. After every item is dragged, it still shows the items and upon trying to drag the item , application get crash as there was no item to be dragged.
Issue is resolved after implementing with ObservableCollection with CollectionChnaged event handler.
previous code which failed:
System.Object[] ItemObject = new System.Object[10];
for (int i = 0; i <= 3; i++)
{
ItemObject[i] = "Item" + i;
}
LT1.Items.AddRange(ItemObject);
Solved with ObservableCollection:
private ObservableCollection<TodoItem> items;
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<TodoItem>()
{
new TodoItem(){TitleText = "Item1"},
new TodoItem(){TitleText = "Item2"},
new TodoItem(){TitleText = "Item3"},
};
lt1.ItemsSource = items;
}
public class TodoItem
{
public string TitleText { get; set; }
}