C# 将项目拖放到treeview时出错

C# 将项目拖放到treeview时出错,c#,asp.net,silverlight,telerik,mvvm-light,C#,Asp.net,Silverlight,Telerik,Mvvm Light,我正在使用Telerik Silverlight RadControls的RadTreeView。我的RadTreeView的XAML如下所示: 我有如下火灾事件: private void OnDropQuery(object sender, DragDropQueryEventArgs e) { RadTreeViewItem destinationItem = e.Options.Destination as RadTreeViewItem;

我正在使用Telerik Silverlight RadControls的RadTreeView。我的RadTreeView的XAML如下所示:


我有如下火灾事件:

  private void OnDropQuery(object sender, DragDropQueryEventArgs e)
    {

            RadTreeViewItem destinationItem = e.Options.Destination as RadTreeViewItem;
            object source = this.GetItemFromPayload<object>(e.Options.Payload);
            object target = destinationItem != null ? destinationItem.Item : null;
            DropPosition position = destinationItem != null ? destinationItem.DropPosition : DropPosition.Inside;

            if (source != null && target != null)
            {
                Section sourceSection = source as Section;
                Section targetSection = target as Section;
                Question sourceQuestion = source as Question;
                Question targetQuestion = target as Question;

                if (sourceSection != null)
                {
                    e.QueryResult = false;
                    return;
                }

                if (sourceQuestion != null)
                {
                    if (sourceQuestion != null && targetQuestion != null && object.ReferenceEquals(sourceQuestion, targetQuestion))
                    {
                        e.QueryResult = false;
                        return;
                    }

                    if (targetQuestion != null && position == DropPosition.Inside)
                    {
                        e.QueryResult = false;
                        return;
                    }

                    if (position != DropPosition.Inside && targetQuestion == null)
                    {
                        e.QueryResult = false;
                        return;
                    }
                }
            }
            else
            {
                e.QueryResult = false;
                return;
            }
            e.QueryResult = true;

    }

    private T GetItemFromPayload<T>(object payload)
    {
            IEnumerable draggedItems = payload as IEnumerable;
            if (draggedItems != null)
            {
                return draggedItems.OfType<T>().FirstOrDefault();
            }

        return default(T);
    }
private void OnDropQuery(对象发送方,DragDropQueryEventArgs e)
{
RadTreeViewItem destinationItem=e.Options.Destination作为RadTreeViewItem;
对象源=this.GetItemFromPayload(e.Options.Payload);
对象目标=destinationItem!=null?destinationItem.Item:null;
DropPosition position=destinationItem!=null?destinationItem.DropPosition:DropPosition.Inside;
if(源!=null&&target!=null)
{
Section sourceSection=源作为节;
区段targetSection=作为区段的目标;
问题来源问题=问题来源;
问题targetQuestion=目标为问题;
if(sourceSection!=null)
{
e、 QueryResult=false;
返回;
}
if(sourceQuestion!=null)
{
if(sourceQuestion!=null&&targetQuestion!=null&&object.ReferenceEquals(sourceQuestion,targetQuestion))
{
e、 QueryResult=false;
返回;
}
if(targetQuestion!=null&&position==DropPosition.Inside)
{
e、 QueryResult=false;
返回;
}
if(position!=DropPosition.Inside&&targetQuestion==null)
{
e、 QueryResult=false;
返回;
}
}
}
其他的
{
e、 QueryResult=false;
返回;
}
e、 QueryResult=true;
}
私有T GetItemFromPayload(对象负载)
{
IEnumerable draggedItems=有效载荷为IEnumerable;
如果(draggedItems!=null)
{
返回draggedItems.OfType().FirstOrDefault();
}
返回默认值(T);
}

但当我试图放弃这个问题时,我得到了一个NullReferenceException。如何解决这个问题?

我解决了它。我只是将此代码放入try catch块并更改了代码:

if (sourceQuestion != null && targetQuestion != null && object.ReferenceEquals(sourceQuestion, targetQuestion))
                    {
                        sourceSection.Questions.Remove(sourceQuestion);
                        targetSection.Questions.Add(sourceQuestion);
                        e.QueryResult = false;
                        return;
                    }

就是这样。它正在工作。

您尚未提供所有代码。假设您有节视图和问题视图模型类-它们包含什么?
OnDropQuery
处理程序是如何连接的?这个
OnDropQuery
方法在哪里?例如,它在包含treeviews的UserControl的代码隐藏中吗?OnDropQuery在childWindow的代码隐藏中。section类包含不同类型的节,如:food section…等。这个问题课包含一些特别的问题,比如:食物新鲜吗?等onDropQuery在构造函数中调用,如:
public main window(){this.treeView1.AddHandler(RadDragAndDropManager.DropQueryEvent,new EventHandler(onDropQuery),true);this.treeView2.AddHandler(RadDragAndDropManager.DropQueryEvent,new EventHandler(onDropQuery),true);}
您仍然没有提供足够的详细信息,我无法重现您的错误。您的
部分
问题
课程的代码在哪里?(如果其中一个出现了错误,那么你就帮不了我们。)另外,你能发布你得到的NullReferenceException的stacktrace吗?最后,请避免在评论中发布太多代码;相反,编辑您的问题以添加代码。