I have made a WPF application that allows drag and drop of instances of a class I made (FakeNodeViewModel
). It works fine when I work with only one instance of my application. But when I use 2 and I try to drag objects from one and drop on the other, I get an exception when dropping:
System.Runtime.InteropServices.COMException: 'Tymed non valide (Exception de HRESULT : 0x80040069 (DV_E_TYMED))'
and the two instances crash. My method to handle drop starts like this:
private void grid_Drop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent("fakeNode")) {
var fakeNodeViewModel = (FakeNodeViewModel)e.Data.GetData("fakeNode");
and the program crash at var fakeNodeViewModel = (FakeNodeViewModel)e.Data.GetData("fakeNode");
I didn't find a lot about this. Does anyone know what happen and how to solve it ?
You should decorate the FakeNodeViewModel
class with the System.SerializableAttribute
for it be COM serializable:
[Serializable]
public class FakeNodeViewModel
{
...
}
Thanks mm8 for your answer. Indeed, I had to add [Serializable]
attribute to my FakeNodeViewModel
class. Then I had to do my drag drop like this:
drag:
private void MoveFakeNode(object sender, MouseEventArgs e) {
if (e.LeftButton == MouseButtonState.Pressed) {
// Current mouse position
var fakeNodeViewModel = (FakeNodeViewModel)((FrameworkElement)e.OriginalSource).DataContext;
var dataObject = new DataObject(DataFormats.Serializable, fakeNodeViewModel);
DragDrop.DoDragDrop((DependencyObject)e.Source, dataObject, DragDropEffects.Copy);
}
}
drop:
private void grid_Drop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Serializable)) {
// Get the fake node
var fakeNodeViewModel =(FakeNodeViewModel)e.Data.GetData(DataFormats.Serializable);
...
}
}
And I was able to get my data from the other instance.
User contributions licensed under CC BY-SA 3.0