将项目放入树视图时出错

时间:2012-02-12 06:13:19

标签: c# asp.net silverlight telerik mvvm-light

我正在使用来自Telerik的Silverlight RadControls的RadTreeView。我的RadTreeView的XAML如下:

       <telerik:RadTreeView x:Name="treeView1" IsDragDropEnabled="True" Margin="0,0,444,0" ItemsSource="{Binding SectionList}">
           <telerik:RadTreeView.ItemTemplate>
                <telerik:HierarchicalDataTemplate x:Name="defaultSectionName" ItemsSource="{Binding Questions, Mode=TwoWay}">
                    <telerik:HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Question1, Mode=TwoWay}"/>
                        </DataTemplate>
                    </telerik:HierarchicalDataTemplate.ItemTemplate>
                </telerik:HierarchicalDataTemplate>
            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>
        <telerik:RadTreeView x:Name="treeView2" IsDragDropEnabled="True" Margin="410,0,0,0" ItemsSource="{Binding SelectedSectionList}" >
            <telerik:RadTreeView.ItemTemplate>
                <telerik:HierarchicalDataTemplate x:Name="SelectedSectionName" ItemsSource="{Binding Questions, Mode=TwoWay}">
                    <telerik:HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Question1, Mode=TwoWay}"/>
                        </DataTemplate>
                    </telerik:HierarchicalDataTemplate.ItemTemplate>
                </telerik:HierarchicalDataTemplate>
            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>

我有下面的火灾事件:

  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);
    }

但是当我试图删除问题时,我得到一个NullReferenceException。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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

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

就是这样。它正在发挥作用。