c# - e.data.GetData 始终为空

标签 c# drag-and-drop dsl vsx visual-studio-2010

我正在使用 Visual Studio 2010 开发扩展

我需要从工具窗口中的 WPF TreeView 拖放到 DSL 图上,但是当我调用 e.data.GetData 时,我无法获得值,想知道我做错了什么

    private void OnDragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(SqlServerTable)))
        {
            try
            {
                SqlServerTable table = (SqlServerTable)e.Data.GetData(typeof(SqlServerTable));
                MessageBox.Show(table.Name);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

第一个 if 语句解析为 True。这会告诉我它是那种对象。 这是 WPF TreeView 中的内容:

        private void DataSourceExplorerTreeView_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (DataSourceExplorerTreeView.SelectedValue is TableViewModel)
            {
                Table table = ((TableViewModel)DataSourceExplorerTreeView.SelectedValue).Table;
                DragDrop.DoDragDrop(DataSourceExplorerTreeView, table, DragDropEffects.Copy);
            }
        }
    }

SqlServerTable 继承自Table。如果我插入一个断点并调用

  e.Data.GetFormats()

我可以看到我的完全限定类型名

最佳答案

我已经能够使用反射解决这个问题:MSDN Forum Answer

        private void OnDragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(SqlServerTable)))
        {
          FieldInfo info;

          object obj;

          info = e.Data.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);

          obj = info.GetValue(e.Data);

          info = obj.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);

         System.Windows.DataObject dataObj = info.GetValue(obj) as System.Windows.DataObject;

         SqlServerTable table = dataObj.GetData("Project.SqlServerTable") as SqlServerTable ;
        }
    }

关于c# - e.data.GetData 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422166/

相关文章:

python - PyQt4 - 将文件拖放到 QPushButton 中

actionscript-3 - Flash AS3 - 将多个对象拖放到一个目标?

python - 如何使用 pyparsing 解析小数表达式?

c# WebAPI Owin jwt RefreshToken invalid_grant 收藏

c# - HttpWebRequest 无法通过代理连接?

c# - 单卡怎么拖

scala - 带尖括号 (<>) 的方法

Scala DSL、对象和中缀表示法

c# - 在不同的 .NET 框架之间共享记录器

c# - 如何在JSON数组中按键获取元素