Export (0) Print
Expand All

TreeViewItem.IsExpanded Property

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)

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.

Dependency property identifier field: IsExpandedProperty

When IsExpanded is set to true, the children of this TreeViewItem are added to the visual tree.

The following example shows how to retrieve an ItemContainerGenerator for a TreeView object and use the IsExpanded property to expand the item.

Run this sample


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>


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft