1

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?

ΩmegaMan
  • 22,885
  • 8
  • 76
  • 94
surpavan
  • 1,262
  • 5
  • 30
  • 61

1 Answers1

4
  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).
H.B.
  • 136,471
  • 27
  • 285
  • 357
  • Could you please tell me in more detail, I am bit new to advanced stuff – surpavan Aug 24 '12 at 21:30
  • @surpavan: It's not advanced, really. See [this post](http://stackoverflow.com/a/4080545/546730) for an example. – H.B. Aug 24 '12 at 21:34
  • That worked and I did this before seeing your link. Hehe. Thanks for the time and for sharing the information is the best way to understand. Many thanks. – surpavan Aug 24 '12 at 21:51