Question

im implementing a very common method for Drag-Drop operation using dependency properties, part of the code is shown below

the problem is, i need to get informations out from the draggedItem (defined as a object class), which is important for other calculations in the application

how can i get access to the data ? is it possible ? or should i define draggedItem as another type? thnx in advance!

private void DropTarget_PreviewDragOver(object sender, DragEventArgs e)
        {
            object draggedItem = e.Data.GetData(this.format.Name);

            DecideDropTarget(e);
            if (draggedItem != null)
            {
                // Dragged Adorner is only updated here - it has already been created in DragEnter.
                ShowDraggedAdorner(e.GetPosition(this.topWindow));
                UpdateInsertionAdornerPosition();
            }
            e.Handled = true;
        }
Was it helpful?

Solution

You have to know exactly what type that your draggedObject is before you can use any of its members. If you have created generic drag and drop functionality, then you need to add a property of type Type that will specify, maybe even validate what type of data is being dragged. If for instance, you knew that your draggedObject was of type Address, then you could simply cast it and then use the properties like this:

object draggedItem = e.Data.GetData(this.format.Name);
Address draggedAddress = (Address)draggedItem;
DoSomethingWith(draggedAddress.PostCode);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top