tvProperties为TreeView的名称。
Private Sub ExpandAll_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
For Each item As Object In tvProperties.ItemsDim treeItem As TreeViewItem = TryCast(tvProperties.ItemContainerGenerator.ContainerFromItem(item), TreeViewItem)
If treeItem IsNot Nothing Then
ExpandAll(treeItem, True)
End If
treeItem.IsExpanded = True
Next
End Sub
Private Sub ExpandAll(item As ItemsControl, expand As Boolean)
For Each obj As Object In item.Items
'如果TreeViewItem未被展开过,则无法通过ItemContainerGenerator来查找这个TreeViewItem下的子节点。
‘所以需要先将TreeViewItem的IsExpanded属性设置为True,并调用UpdateLayout方法。
<