【问题标题】:NullReferenceException when using converter in ItemSource of HierarchicalDataTemplate在 HierarchicalDataTemplate 的 ItemSource 中使用转换器时出现 NullReferenceException
【发布时间】:2014-07-09 13:12:30
【问题描述】:

我有一个包含 TreeViewItems 的树视图。这些 TreeViewItems 可能包含在其项目中也是 TreeViewItems 的子项。我希望所有项目在根级别以及所有子级别(我的实现中有 0 或 1)按字母顺序排序。

树视图配置如下:

<controls:TreeViewEx Name="m_tvLeft" 
                     ItemsSource="{Binding LeftColumnItems, Converter={x:Static cv:AlphabeticListSortConverter.Instance}, ConverterParameter=Header}"
                     SelectionChanged="OnTvLeftSelectionChanged"
                     MouseDoubleClick="OnTvLeftItemMouseDoubleClick">
   <controls:TreeViewEx.Resources>
      <Style TargetType="controls:TreeViewExItem">
         <Setter Property="FontSize" Value="11"/>
      </Style>
   </controls:TreeViewEx.Resources>
   <controls:TreeViewEx.ItemContainerStyle>
      <Style TargetType="controls:TreeViewExItem">
         <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
      </Style>
   </controls:TreeViewEx.ItemContainerStyle>
   <controls:TreeViewEx.ItemTemplate>
      <!-- Template for root fields -->
      <HierarchicalDataTemplate ItemsSource="{Binding Items, Converter={x:Static cv:AlphabeticListSortConverter.Instance}, ConverterParameter=Header}">
         <TextBlock Text="{Binding Header}"  Tag="{Binding}" />
         <!-- Template for child fields -->
         <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
               <TextBlock Text="{Binding Header}" Tag="{Binding}" />
            </DataTemplate>
         </HierarchicalDataTemplate.ItemTemplate>
      </HierarchicalDataTemplate>
   </controls:TreeViewEx.ItemTemplate>
</controls:TreeViewEx>

绑定在这个属性上:

public ObservableCollection<TreeViewItem> LeftColumnItems { get; private set; }

一个叶子项是这样填充的:

TreeViewItem newItem = new TreeViewItem {
                            Header = newColInfo.Name,
                            Tag = newColInfo
                        };

对于有子项的分组项也是这样:

TreeViewItem newGroupItem = new TreeViewItem {
                    Header = columnGroup.GroupName,
                    IsExpanded = columnGroup.IsExpanded
                };

转换器实现:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
   IList collection = value as IList;
   if (collection == null) {
      throw new NotSupportedException(string.Format("The value must be of type IList. Type was {0}", value == null ? "NULL" : value.GetType().ToString()));
   }
   if (parameter == null) {
      throw new NotSupportedException("The parameter must be not NULL.");
   }

   ListCollectionView view = new ListCollectionView(collection);
   SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
   view.SortDescriptions.Add(sort);

   return view;
}

问题:这里会抛出 NullReferenceException view.SortDescriptions.Add(sort);,即使我这边的所有对象都不是 null。如果我仅将转换器添加到树视图的 ItemSource 中,则不会发生这种情况,但随后子节点未排序。

我不明白这个 NullReferenceException 来自哪里,因为我只在 TreeViewItems 上为 Header 属性设置了值。

你明白发生了什么吗?

【问题讨论】:

  • 尝试添加一些手表,即使将鼠标悬停在变量上也可以显示实际为空的内容,如果它发生在view.SortDescriptions.Add(sort);,则view 很有可能为空,但看起来像这很奇怪。
  • @KingKing 视图不能为空,它只是被实例化了
  • @franssu 所以我说这很奇怪,所以你能说什么在这里可能是空的?还是只是一个内在的例外?

标签: wpf sorting treeview


【解决方案1】:

所有集合都有一个默认的CollectionView。对于所有实现IList 的集合,ListCollectionView 对象是默认视图对象。要获得默认视图,请使用 GetDefaultView 方法。

ListCollectionView 继承了实现ICollectionViewCollectionView,因此以ICollectionView 检索任何集合的视图并应用排序、过滤等更安全。

我不确定其余的实现,但使用 ICollectionView 您可以进行更通用的排序

例子

        ICollectionView view = CollectionViewSource.GetDefaultView(collection);
        SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
        view.SortDescriptions.Add(sort);

如果在此之后您收到 view 为 null 然后请检查变量集合是否实际上是一个集合。

更多信息

ListCollectionView

How to: Get the Default View of a Data Collection

【讨论】:

  • 这行得通,谢谢!我还是不明白为什么
  • ListCollectionView 应该使用GetDefaultView 方法检索。其次ListCollectionView继承了CollectionView,它实现了ICollectionView,因此将任何集合的视图检索为ICollectionView并应用排序、过滤等更安全更多信息Get the Default View of a Data Collection
猜你喜欢
  • 1970-01-01
  • 2011-07-13
  • 2012-06-25
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多