2

我做了一些研究,发现了这个:

DataObject d = new DataObject();
d.SetData(DataFormats.Serializable, myObject);
d.SetData(DataFormats.Text, myObject.ToString());
myForm.DoDragDrop(d, DragDropEffects.Copy);

在 win 表单中拖放的代码片段。

我尝试像这样实现它(WPF):

private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DataObject d = new DataObject();
                d.SetData(DataFormats.Serializable, listView1.SelectedItem);
                d.SetData(DataFormats.Text, listView1.SelectedItem.ToString());
                DragDrop.DoDragDrop(listView1, d, DragDropEffects.Copy);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

现在我想,当我将 ListViewItem 拖放到记事本中时,它可能会复制所选项目的类名(因为这就是 listView1.SelectedItem.ToString())...但是记事本却显示了取消符号的图片,而悬停,当我松开鼠标按钮时没有复制任何内容。

这样做的总体目标是将类更改为逗号分隔的字符串,因此当它复制粘贴到记事本时,该类的所有数据都将采用良好的格式。

但是,如果有人可以帮助我复制类名,我相信我可以从那里弄清楚:o

4

1 回答 1

3

所以....是的。

  bool alreadycopying = false;

    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                alreadycopying = false;
            }


            if (e.LeftButton == MouseButtonState.Pressed && alreadycopying == false)
            {
                alreadycopying = true;
                System.IO.StreamWriter test = new System.IO.StreamWriter(@"C:\SuperSecretTestFile.txt");
                test.WriteLine("Test");
                test.Close();

                List<String> testlist = new List<string>();
                testlist.Add(@"C:\SuperSecretTestFile.txt");

                DataObject d = new DataObject();
                d.SetData(DataFormats.FileDrop, testlist.ToArray<string>());
                DragDrop.DoDragDrop(listView1, d, DragDropEffects.All);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

在多次抨击糟糕的记事本技术之后,c#取得了胜利<.<

于 2012-11-09T14:42:06.887 回答