-1

I have multiple ObservableCollection<T>s that are bound to a TreeView using a HierarchicalDataTemplate similar to the implementation found in How to mix databound and static levels in a TreeView?.

My problem is one (or more) of the elements in the collections can be null on occasion but they are still displayed in the TreeView as a blank line, such as in this example:

Example of empty lines

Other than removing them from the collection, is there a method to hiding these elements so they will not appear in the User Interface until they are changed to a non-null value?

If the structure of the XAML is relavant, this is roughly what I am doing:

<TreeView.Resources>
  <HierarchicalDataTemplate DataType="{x:Type local:FolderNode}" 
                            ItemsSource="{Binding Items}">
    <TextBlock Text="{Binding Path=DisplayName}" />
  </HierarchicalDataTemplate>

  <HierarchicalDataTemplate DataType="{x:Type local:ParentItem}">
    <HierarchicalDataTemplate.ItemsSource>
      <MultiBinding>
        <MultiBinding.Converter>
          <local:MultiCollectionConverter />
        </MultiBinding.Converter>
        <Binding Path="Children1" />
        <Binding Path="Children2" />
      </MultiBinding>
    </HierarchicalDataTemplate.ItemsSource>
    <TextBlock Text="{Binding Path=DisplayName}" />
  </HierarchicalDataTemplate>

  <HierarchicalDataTemplate DataType="{x:Type local:ChildItemWithChildCollection}" 
                            ItemsSource="{Binding}">
    <TextBlock Text="{Binding Path=DisplayName}" />
  </HierarchicalDataTemplate>

  <HierarchicalDataTemplate DataType="{x:Type local:ChildItemWithChild}" 
                            ItemsSource="{Binding Path=GrandChildren}">
    <TextBlock Text="{Binding Path=DisplayName}" FontWeight="Bold" />
  </HierarchicalDataTemplate>

  <DataTemplate DataType="{x:Type local:GrandChildItem}">
    <TextBlock x:Name="Item" Text="{Binding Path=DisplayName}" />
  </DataTemplate>

  <DataTemplate DataType="{x:Type local:ChildItemWithoutChildCollection}">
    <TextBlock Text="{Binding Path=DisplayName}" />
  </DataTemplate>

</TreeView.Resources>
Community
  • 1
  • 1
psubsee2003
  • 8,563
  • 8
  • 61
  • 79

1 Answers1

2

If it's whole collection item that is null you can collapse TreeViewItem when DataContext is null

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>
dkozl
  • 32,814
  • 8
  • 87
  • 89