好的,你去:
您必须做三件事才能在 WPF 中启用拖放功能:
- 告诉元素支持拖放
- 设置
DragOver
事件
- 设置
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 文档。