Question

I am using a class as follows:

Class DirectoryViewItem
    Property Namee As String
    Property Iconn As BitmapImage
    Property Path As String
    Property SubNodes As New List(Of DirectoryViewItem)
End Class

and the xaml I used is:

<TreeView Name="DirectoryTreeView"
          TreeViewItem.Expanded="DirectoryTreeView_Expanded"
          Grid.Row="0">
    <TreeView.ItemTemplate >
        <HierarchicalDataTemplate ItemsSource="{Binding SubNodes}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Iconn}"
                       Width="32" Height="32"
                       VerticalAlignment="Center" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding Namee}"
                           VerticalAlignment="Center" HorizontalAlignment="Left" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

The code is working fine, now I want to expand the 3 or some x node through code, and I found the solution to use something like this:

CType(DirectoryTreeView.Items(3), TreeViewItem).ExpandSubtree()

I found that the CType here is not TreeViewItem but it is my DirectoryViewItem type, ... how can this be done?

Was it helpful?

Solution

  1. Use the TreeView.ItemContainerStyle to bind IsExpanded to a property on your items.
  2. Implement ExpandSubtree on your items (all it needs to do is set that bound property on your items to true recursively).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top