将项目放入树视图时出错
本文关键字:视图 出错 项目 | 更新日期: 2023-09-27 18:24:05
我正在使用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);
}
但当我试图删除Question时,我会得到一个NullReferenceException。如何解决此问题?
我解决了它。我只是把这个代码放在try-catch块中,并更改了代码:
if (sourceQuestion != null && targetQuestion != null && object.ReferenceEquals(sourceQuestion, targetQuestion))
{
sourceSection.Questions.Remove(sourceQuestion);
targetSection.Questions.Add(sourceQuestion);
e.QueryResult = false;
return;
}
就是它。它正在工作。