TreeViewItem.IsExpanded Property

Definition

Gets or sets whether the nested items in a TreeViewItem are expanded or collapsed.

C#
public bool IsExpanded { get; set; }

Property Value

true if the nested items of a TreeViewItem are visible; otherwise, false. The default is false.

Examples

The following example shows how to set the IsExpanded property.

XAML
<TreeViewItem Header="Employee1"
              Name="Employee1Data" 
              IsExpanded="True" 
              Collapsed="OnCollapsed" 
              Expanded="OnExpanded">
  <TreeViewItem Header="Work Days" 
                Name="EmployeeWorkDays"
                IsSelected="True">
    <TreeViewItem Header="Tuesday" />
    <TreeViewItem Header="Friday"/>
  </TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Employee2" 
              Name="Employee2Data">
  <TreeViewItem Header="Work Days" 
                Name="emp2WorkDays" 
                Selected="GetSchedule"
                Unselected="SetSchedule">
    <TreeViewItem Header="Monday" />
    <TreeViewItem Header="Wednesday"/>
  </TreeViewItem>
</TreeViewItem>
C#
Employee1Data.IsExpanded = true;

Remarks

The appearance of the button that expands and collapses the TreeViewItem changes when the TreeViewItem is expanded or collapsed. When the IsExpanded property value changes from true to false, the Collapsed event occurs. Similarly, the Expanded event occurs when the IsExpanded property value changes from false to true.

Dependency Property Information

Item Value
Identifier field IsExpandedProperty
Metadata properties set to true None

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also