ComponentOne Studio for WPF provides a very useful class C1DragDropManager which makes the drag/ drop operations very easy to handle. We can easily configure the class C1DragDropManager and instantly add the drag/drop feature to our application. This class is used to customize the drag/ drop operations by validating the drag/ drop operations and allowing move and copy actions. Our objective of writing this blog is to discuss the dragging of rows between two C1DataGrid controls. C1DataGrid allows the end user to select any row from one C1DataGrid and simply drag/ drop them to another C1DataGrid. A visual drag cue is provided when the user moves the mouse from the selected row and visual cues are also provided to indicate the drop location. Implementation begins with setting up the ItemsSource in C1DataGrid controls:-
c1DataGrid1.ItemsSource = Person.Generate(10);
c1DataGrid2.ItemsSource = Person.Generate(10);
Next we need to add a C1DragDropManager control.
C1DragDropManager ddm = new C1DragDropManager();
Once we have the C1DragDropManager control, we set up both the C1DataGrid's as drag source and drop target.
c1DataGrid1.LoadedRowPresenter += (s, e) =>
{
ddm.RegisterDropTarget(e.Row.Presenter, true);
ddm.RegisterDragSource(e.Row.Presenter, DragDropEffect.Copy, ModifierKeys.None);
};
c1DataGrid2.LoadedRowPresenter += (s, e) =>
{
ddm.RegisterDropTarget(e.Row.Presenter, true);
ddm.RegisterDragSource(e.Row.Presenter, DragDropEffect.Copy, ModifierKeys.None);
};
The next step is to subscribe to the DragStart and DragDrop events to do the required processing there.
void DragStart(object source, DragDropEventArgs e)
{
C1.WPF.DataGrid.DataGridRow row = ((DataGridRowPresenter)e.DragSource).Row;
string mark = row.DataItem.ToString();
Border border = ddm.TargetMarker as Border;
border.Height = 32d;
border.Child = new ContentControl()
{
Foreground = new SolidColorBrush(Colors.Black),
FontSize = 16d,
FontWeight = FontWeights.DemiBold,
Content = mark
};
}
void DragDrop(object source, DragDropEventArgs e)
{
var source_row = ((DataGridRowPresenter)e.DragSource).Row;
var source\_table = source\_row.DataGrid;
var target_row = ((DataGridRowPresenter)e.DropTarget).Row;
var target\_table = target\_row.DataGrid;
if (source\_table != target\_table)
{
((ObservableCollection<Person>)source\_table.ItemsSource).Remove((Person)source\_row.DataItem);
((ObservableCollection<Person>)target\_table.ItemsSource).Add((Person)source\_row.DataItem);
}
else
System.Media.SystemSounds.Beep.Play();
}
Download the attached sample applications for complete implementation. DownloadSample_CS DownloadSample_VB