I have an WPF-Datagrid
where I can drop an element. This is a Textelement dropped from an .txt file (open for example with notepad++). Is there any possibility to get information about the .txt file on my Drop-Event ?
我有一个WPF-Datagrid,我可以删除一个元素。这是从.txt文件中删除的Textelement(例如用notepad ++打开)。有没有可能在我的Drop-Event上获取有关.txt文件的信息?
Edit:
void OnDragDrop(object sender, DragEventArgs e)
{
String text = e.Data.GetData(DataFormats.Text, true);
}
Here I can get the Text of my drop element, but I found no solution to get the source file, where the drag get started.
在这里,我可以获取我的drop元素的Text,但是我找不到获取源文件的解决方案,拖动开始的位置。
2 个解决方案
#1
0
ok here you go:
好的,你去吧:
You have to do three things to enable drag'n drop in WPF:
你必须做三件事才能在WPF中实现拖放:
- Tell the element to support drag'n drop
- Setup a
DragOver
Event - Setup a
Drop
Event
告诉元素支持拖放
设置DragOver事件
设置丢弃事件
ok, let's first look at the XAML:
好的,我们先来看看XAML:
<DataGrid AllowDrop="True"
DragOver="DataGrid_DragOver"
Drop="DataGrid_Drop"/>
And the event handler code:
和事件处理程序代码:
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
/* ... */
}
}
For more information see the MSDN documentation.
有关更多信息,请参阅MSDN文档。
#2
0
No.
Think of Drag & Drop the same way as Cut & Paste -- typically only the "data" is being dragged over during the event, and no additional metadata about its source.
可以想象拖放与剪切和粘贴相同 - 通常只在事件期间拖动“数据”,并且没有关于其源的其他元数据。
One exception to this, is when dragging text from a web page. DataFormats.Html
will include the SourceURL that the text came from.
一个例外是从网页拖动文本时。 DataFormats.Html将包含文本来自的SourceURL。
#1
0
ok here you go:
好的,你去吧:
You have to do three things to enable drag'n drop in WPF:
你必须做三件事才能在WPF中实现拖放:
- Tell the element to support drag'n drop
- Setup a
DragOver
Event - Setup a
Drop
Event
告诉元素支持拖放
设置DragOver事件
设置丢弃事件
ok, let's first look at the XAML:
好的,我们先来看看XAML:
<DataGrid AllowDrop="True"
DragOver="DataGrid_DragOver"
Drop="DataGrid_Drop"/>
And the event handler code:
和事件处理程序代码:
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
/* ... */
}
}
For more information see the MSDN documentation.
有关更多信息,请参阅MSDN文档。
#2
0
No.
Think of Drag & Drop the same way as Cut & Paste -- typically only the "data" is being dragged over during the event, and no additional metadata about its source.
可以想象拖放与剪切和粘贴相同 - 通常只在事件期间拖动“数据”,并且没有关于其源的其他元数据。
One exception to this, is when dragging text from a web page. DataFormats.Html
will include the SourceURL that the text came from.
一个例外是从网页拖动文本时。 DataFormats.Html将包含文本来自的SourceURL。