2009-05-22 11 views
1

ユーザーが特定の行のデータを1つのカスタムリストコントロールから別のカスタムコントロールにドラッグアンドドロップできるようにしようとしています。この場合、2番目のリストコントロールは同じアプリケーションの別のインスタンスにあります。インスタンス間でデータをドラッグアンドドロップする

parameterTypedListView.SelectedObjects Tはフィールド/プロパティとしてのみ値型を含むカスタムクラスであるジェネリックのIListです
DoDragDrop(parameterTypedListView.SelectedObjects, DragDropEffects.Copy); 

OnDragDropイベントでは、このデータを抽出しようとしましたが、System.MarshalByRefObjectから継承しているようなSystem.__ComObject ...オブジェクトしか取得しません。

要約:実際に使用できるオブジェクト指向の形式でデータを抽出するにはどうすればよいですか?

編集:私のカスタムクラスをシリアライズ可能に設定しても、何の影響もありません。

foreach (var dataObject in (IEnumerable) e.Data.GetData("System.Collections.ArrayList")) 
{ 
    // this actually enumerates the correct number of times, i.e. as many times as there are items in the list. 
} 

しかし、すべてのDataObjectは、私が何か役に立つにキャストすることができないということ、それ自体が、システム.__ ComObjectです:私は__ComObjectを列挙することができます。

+0

...インスタンス間で動作しますか? – Konstantinos

+0

@ Constantinos、はい、同じアプリケーションの複数のインスタンス。 –

答えて

3

あなたの最初の問題を再現できましたが、配列リストのクラスに[Serializable]属性を追加すると、そのオブジェクトが正しいタイプであることがわかりました。

小さなコード例を示します。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop); 
     this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); 
    } 

    [Serializable] 
    class DragClass 
    { 
     public string Prop1 { get; set; } 
     public int Prop2 { get; set; } 
    } 

    private void label1_MouseDown(object sender, MouseEventArgs e) 
    { 
     System.Collections.ArrayList aDragClasses = new System.Collections.ArrayList(); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test1", Prop2 = 2 }); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test2", Prop2 = 3 }); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test3", Prop2 = 4 }); 

     DoDragDrop(aDragClasses, DragDropEffects.Copy); 
    } 

    private void Form1_DragEnter(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 

    private void Form1_DragDrop(object sender, DragEventArgs e) 
    { 
     foreach (var aData in (System.Collections.IEnumerable)e.Data.GetData(typeof(System.Collections.ArrayList))) 
     { 
      System.Diagnostics.Debug.WriteLine(((DragClass)aData).Prop1); 
     } 
    } 



} 
0

私は問題があなたが直接データを渡すためにリストを使用していると思います。私はそれを失敗させるいくつかの異なる方法を試し、それがうまくいかないいくつかの方法を考え出しました。

カスタムクラスに[Serializable]属性がない場合、クラスがプロセス間でマーシャリングされる方法であるため、正しく動作しません。また、リストを直接使用してデータを渡すと、null参照例外が発生します。

単純なトランスポートクラスを使用してデータを渡すと(すべての型がシリアライズ可能な場合)、すべてが正常に機能しました。

[Serializable] 
class Test 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

[Serializable] 
class Transport 
{ 
    public Transport() 
    { 
     this.Items = new List<Test>(); 
    } 
    public IList<Test> Items { get; private set; } 
} 

それから私は、これは何の問題もできないし、それは我々が2つの異なるインスタンスに二回実行されている同じプログラムについて話している

private void Form1_DragDrop(object sender, DragEventArgs e) 
{ 
    foreach (var item in ((Transport)e.Data.GetData(typeof(Transport))).Items) 
    { 
     System.Diagnostics.Debug.WriteLine(item.Name + " " + item.Description); 
    } 
} 
関連する問題