【发布时间】:2020-01-22 18:40:48
【问题描述】:
我有一个 radGridView,我可以将文件从 Windows 资源管理器拖放到其中,也可以从其中将文件拖到资源管理器中。
但是,我需要禁止拖入自身(复制条目并生成异常)。
WPF:
<telerik:RadGridView Name="radGridView" Drop="OnDrop" DragLeave="radGridView_DragLeave">
<telerik:RadGridView.RowStyle>
<Style TargetType="telerik:GridViewRow">
<Setter Property="telerik:DragDropManager.AllowDrag" Value="True" />
</Style>
</telerik:RadGridView.RowStyle>
和处理程序:
private void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
MessageManager.Publish("LoadDataFiles", files);
}
}
private void radGridView_DragLeave(object sender, DragEventArgs e)
{
[..]
DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, paths.ToArray()),
DragDropEffects.Copy);
}
此代码确实有效,但是,我未能避免拖放到自身中。另外,放入文件资源管理器时的图标也不对。
值得注意的是 OnDrop 有时即使文件被放入资源管理器中也会被调用。
从gridview拖放到文件资源管理器后有时也会产生异常,异常发生在DragDrop行:
托管调试助手 'FatalExecutionEngineError' : '运行时 遇到致命错误。错误的地址在 0x70cca7d0,在线程 0x48c8 上。错误代码为 0x80131623。这个错误 可能是 CLR 中的错误,或者是不安全或不可验证的部分 用户代码。此错误的常见来源包括用户编组错误 用于 COM 互操作或 PInvoke,这可能会损坏堆栈。'
【问题讨论】:
标签: c# .net wpf drag-and-drop