2012-02-12 1 views
0

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を取得します。この問題を解決するにはどうすればよいですか?

+0

すべてのコードを提供しているわけではありません。おそらくあなたはセクションと質問のビューモデルクラスを持っています - それらは何を含んでいますか? 'OnDropQuery'ハンドラはどのように配線されていますか?このOnDropQueryメソッドはどこにありますか?たとえば、ツリービューを含むUserControlのコードビハインドですか? –

+0

OnDropQueryはchildWindowのコードの背後にあります。セクションクラスには、次のようなセクションの異なるタイプが含まれます。質問クラスには、perticularセクションの賢明な質問が含まれています。食べ物は新鮮ですか? onDropQueryは、次のようにコンストラクタで呼び出されます: 'public MainWindow(){this.treeView1.AddHandler(RadDragAndDropManager.DropQueryEvent、new EventHandler (OnDropQuery)、true); this.treeView2.AddHandler(RadDragAndDropManager.DropQueryEvent、新しいイベントハンドラ(OnDropQuery)、true); } ' – R76

+0

エラーを再現できるほどの詳細はまだ私には提供されていません。 'Section'クラスと' Question'クラスのコードはどこにありますか? (バグがあった場合は、私たちを助けてくれません)また、取得しているNullReferenceExceptionのスタックトレースを投稿できますか?最後に、コメントにあまりにも多くのコードを投稿することは避けてください。代わりに、質問を編集してコードを追加してください。 –

答えて

0

私はそれを解決しました。私はこのコードをtry catchブロックに入れ、コードを変更しました。

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

これはそれです。それは働いている。

関連する問題