Drag-drop operations between two sources are very common. Silverlight suite from ComponentOne provides a pretty useful class C1DragDropManager which makes the drag/drop pretty easy to handle. You can easily configure the class and instantly add the drag/drop feature to you application. Lets see how you can enable Drag/drop operation between two C1FlexGrid controls in Silverlight**. The 'Row' object used by C1FlexGrid is not a native UIElement, so we need to register the border elements used by C1FlexGrid in cells as 'drag source'. This is done in a custom CellFactory** class. See the code below:-
public class DragDropCellFactory : CellFactory
{
private C1DragDropManager _ddm;
public DragDropCellFactory(C1DragDropManager ddm)
{
_ddm = ddm;
}
public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)
{
base.CreateCellContent(grid, bdr, rng);
bdr.Tag = grid.Rows[rng.Row]; //used to find the actual row dragged
_ddm.RegisterDragSource(bdr, DragDropEffect.Move, ModifierKeys.None);
}
}
The next step is to subscribe to the DragEnter and DragDrop events to do the required processing there.
ddm.DragStart += (s, e) =>
{
var bdr = e.DragSource as Border;
var row = (bdr.Tag as Row);
var flex = (bdr.Tag as Row).Grid;
ddm.TargetMarker.Width = flex.ActualWidth; //configure the Target Marker
};
ddm.DragDrop += (s, e) =>
{
var bdr = e.DragSource as Border;
var flex = e.DropTarget as C1FlexGrid;
var row = flex.HitTest(e.GetPosition(flex)).Row;
if (flex.Name == "flex1") //check the actual drop target
{
flex1source.Insert(row + 1, (bdr.Tag as Row).DataItem as Customer);
flex2source.RemoveAt((bdr.Tag as Row).Index);
}
else if (flex.Name == "flex2")
{
flex2source.Insert(row + 1, (bdr.Tag as Row).DataItem as Customer);
flex1source.RemoveAt((bdr.Tag as Row).Index);
}
};
Download the attached sample for complete implementation. C# Sample