【问题标题】:Drag Dropping from WPF to Notepad从 WPF 拖放到记事本
【发布时间】:2012-11-09 13:12:06
【问题描述】:

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

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

编码 sn-p 以在 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

【问题讨论】:

  • 我会尝试放弃第一个 setdata 调用。
  • 是的,我想这有点多余哈哈,仍然不起作用
  • 您确定记事本允许删除文本项吗?我看到我可以将文件拖放到记事本中,它会打开并读入,但我似乎无法拖放文本。
  • 哦.... 什么鬼?它允许删除整个文件,但不允许删除文本?
  • 可能有一种方法可以将我选择的列表视图项转换为文本文件然后将其删除?

标签: c# wpf drag-and-drop


【解决方案1】:

所以....是的。

  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#取得了胜利<.>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2017-08-06
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多