Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
695
drag and drop within xamDataGrid
posted

I am trying to implement the drag and drop functionality within a xamDataGrid. I have all the event handlers hooked up. In the drop event handler i have following code:

void xamDataGrid2_Drop(object sender, DragEventArgs e)
        {
            XamDataGrid dragSource2 = xamDataGrid;
            XamDataGrid dragTarget = sender as XamDataGrid;

            //Get Instance of the dragger DataRecordPresenter and DataRecord
            DataRecordPresenter draggedPresenter = e.Data.GetData(typeof(DataRecordPresenter)) as DataRecordPresenter;
            DataRecord draggedRecord = draggedPresenter.Record as DataRecord;

            // If there are not items in the Drag Target, just add the dragged record
            if (dragTarget.Records.Count == 0)
            {
                dragTarget.DataItems.Add(draggedRecord.DataItem);
                dragSource2.ActiveRecord = null;
                return;
            }
            // There are items already in the Drag Target, so we need to determine where to insert the dragged record
            // depending on the Mouse position.
            else
            {
                DataRecord existingRecord = dragTarget.Records[0] as DataRecord;
                // Records have to be of the same type to be in the same FieldLayout
                if (existingRecord.DataItem.GetType() == draggedRecord.DataItem.GetType())
                {
                    // Insert Logic based on the Mouse position over the Drag Target
                    DataRecordPresenter tempPresenter = DataRecordPresenter.FromRecord(dragTarget.Records[0]) as DataRecordPresenter;
                    Point positionOfDrop = e.GetPosition(dragTarget);
                    int i = GetRecordInsertPosition(positionOfDrop, tempPresenter);
                    dragTarget.DataItems.Insert(i, draggedRecord.DataItem);

                    // force the ActiveRecord to null so that an erroneous drag in the Adorner will not be performed
                    dragSource2.ActiveRecord = null;
                }
            }
        }

but i got exception here: dragTarget.DataItems.Insert(i, draggedRecord.DataItem);

The dragTarget.DataItems is 0, but I do have dragTarget.Records.Count =4 (i have 4 records in the grid). If I use 2 xamDatagrid and drag from grid 1 to grid 2, dragTarget.DataItems would show the correct number of data items. It's not working from within one xamDatagrid ... why is this?