我正在使用 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/