Home > front end >  How do I access object held inside of dataobject?
How do I access object held inside of dataobject?

Time:01-05

Involves using Gong DragDrop WPF NuGet (but may not affect the typical functionality of a drag & drop operation even without).

Setup:

  1. A PreviewDrop event that handles a ListBox.
  2. A DataObject variable.
  3. Assign e.Data to DataObject variable
  4. Drag and Drop object from any place to this ListBox in order to trigger.
  5. Debug breakpoint to see DataObject in Locals.

What I need to get:

The Content of the ListBoxItem or the Header of the TreeViewItem that was dragged into it (Gong lets you do this) OR the whole object that was dropped in my ListBox.

I can physically see that this DataObject contains the dropped object in its Non-Public Members -> InnerData -> Non-Public Members -> Data -> Raw View -> Values -> Results View -> (0) -> (0) -> Data but I do not know how to access it.

What I have tried:

Googled overflow and codeproject, VB.Net documentation, found nothing specific on the issue (or maybe it was too vague).

I tried using Reflection to get the InnerData field like this:

Dim myElement = GetType(DataObject).GetField("innerData", System.Reflection.BindingFlags.NonPublic).GetValue(e.Data), 

however I get System.NullReferenceException: 'Object reference not set to an instance of an object.'.

Dim myInnerData = myData.GetType().GetField("_innerData", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance) 

will get my innerData, however I lose the "Data" of "innerData", leaving me still unable to get the object that is attached to the DataObject.

CodePudding user response:

The solution is much simpler than I thought. Joe had the sent me the right way to begin with, to explain:

You need to get the DataObject.GetFormats to know what you are working with, copy the string that GetFormats returns, something like this:

Dim myElement = myData.GetFormats

Which for me returns:

Output

Pass that string to GetData like this:

Dim myInnerData = myData.GetData("GongSolutions.Wpf.DragDrop")

And voila, you just got the object of the DragEventArgs.

As far as I know this is the only topic that has an answer regarding this specific problem.

  •  Tags:  
  • Related