TreeViewItem.IsExpanded Property
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Gets or sets a value that indicates whether the Items contained by this TreeViewItem are expanded or collapsed.
Namespace: System.Windows.Controls
Assembly: System.Windows.Controls (in System.Windows.Controls.dll)
Syntax
'Declaration
Public Property IsExpanded As Boolean
public bool IsExpanded { get; set; }
<sdk:TreeViewItem IsExpanded="bool"/>
Property Value
Type: System.Boolean
true to indicate the contents of the Items collection are expanded; false to indicate the items are collapsed. The default is false.
Remarks
Dependency property identifier field: IsExpandedProperty
When IsExpanded is set to true, the children of this TreeViewItem are added to the visual tree.
Examples
The following example shows how to retrieve an ItemContainerGenerator for a TreeView object and use the IsExpanded property to expand the item.
Public Sub New()
InitializeComponent()
Dim myItems As String() = New String() {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
myTreeView.DataContext = myItems
End Sub
Shared count As Integer = 1
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim item As TreeViewItem = DirectCast(myTreeView.ItemContainerGenerator.ContainerFromIndex(3), TreeViewItem)
item.IsExpanded = True
If count < 5 Then
item.Items.Add("Child " & count.ToString())
count += 1
End If
End Sub
public MainPage()
{
InitializeComponent();
string[] myItems = new string[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
myTreeView.DataContext = myItems;
}
static int count = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromIndex(3);
item.IsExpanded = true;
if (count < 5)
{
item.Items.Add("Child " + count.ToString());
count++;
}
}
<StackPanel x:Name="LayoutRoot" Background="White">
<sdk:TreeView x:Name="myTreeView" Width="200" ItemsSource="{Binding}" Margin="5"/>
<Button Content="Add Child to Item 4" Width="150" Click="Button_Click"/>
</StackPanel>
Version Information
Silverlight
Supported in: 5, 4, 3
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.