0

我有一个WPF-Datagrid可以放置元素的地方。这是从.txt文件中删除的文本元素(例如使用 notepad++ 打开)。是否有可能在我的 Drop-Event 上获取有关 .txt 文件的信息?

编辑:

void OnDragDrop(object sender, DragEventArgs e)
{
    String text = e.Data.GetData(DataFormats.Text, true);
}

在这里我可以获取我的放置元素的文本,但我没有找到获取源文件的解决方案,从那里开始拖动。

4

2 回答 2

0

好的,你去:

您必须做三件事才能在 WPF 中启用拖放功能:

  1. 告诉元素支持拖放
  2. 设置DragOver事件
  3. 设置Drop事件

好的,我们先来看看XAML:

<DataGrid AllowDrop="True"
            DragOver="DataGrid_DragOver"
            Drop="DataGrid_Drop"/>

和事件处理程序代码:

private void DataGrid_DragOver(object sender, DragEventArgs e)
{
    // check if the element dragged over is one or more files
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        // if so, show a link cursor
        e.Effects = DragDropEffects.Link;
    }
    else
    {
        // otherwise show a "block" cursor
        e.Effects = DragDropEffects.None;
    }

    // IMPORTANT: mark the event as "handled by us", to apply the drag effects
    e.Handled = true;
}

private void DataGrid_Drop(object sender, DragEventArgs e)
{
    // Check if the data dropped is one or more files
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        // get the file pathes from the data object
        string[] filePaths = (e.Data.GetData(DataFormats.FileDrop) as string[]); 

        // do something with the pathes
        /* ... */
    }
}

有关详细信息,请参阅MSDN 文档

于 2012-07-27T13:36:20.603 回答
0

不。

以与剪切和粘贴相同的方式考虑拖放- 通常只有“数据”在事件期间被拖动,并且没有关于其源的其他元数据。

一个例外是从网页中拖动文本时。DataFormats.Html将包括文本来自的 SourceURL。

于 2015-02-08T21:56:06.713 回答