Working with WPF Expander Control using C# and XAML

The Expander class in C# represents a WPF Expander control. At design-time, the <Expander> element of XAML is used to create an Expander control. The code examples in this tutorial demonstrate how to use an expander control in a WPF app.

The WPF Expander represents a control with an expanded view where the contents of the expanded area can be expanded or collapsed. There are two ways to create an expander control in a WPF app. We can create an Expander control at design-time using the <Expander> element of XAML. We can also use the Expander class in C# to create an expander at run-time. 

WPF Expander

The following XAML code sample shows how to create an Expander control in WPF.
  1. <Expander Name="ExpanderControl" Header="Click to Expand"   
  2.           HorizontalAlignment="Left" >  
  3.     <TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Foreground="Black">  
  4.         This is an Expander control. Within this control, all contents will be wrapped.  
  5.         At run-time, you may expand or collapse this control. Type more text here to be typed.  
  6.         Jump around and hype.   
  7.     </TextBlock>  
  8. </Expander>  
The default view of an Expander control looks like this.


If you click on the header, the expanded view looks like this.



Most of the time, you would want the header of the Expander control to look different from the contents. This can be done by simply setting the font properties of the Expander control different from the contents.

This code sets the FontSize and FontWeight of the Expander control different from the contents of the Expander control. 
  1. <Window x:Class="ExpanderControlSample.Window1"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="Window1" Height="300" Width="300">  
  5.     <Grid>  
  6.         <Expander Name="ExpanderControl" Background="LavenderBlush"   
  7.           HorizontalAlignment="Left" Header="Click to Expand"   
  8.           ExpandDirection="Down" IsExpanded="False" Width="200"  
  9.                   FontSize="20" FontWeight="Bold" Foreground="Green">  
  10.             <TextBlock TextWrapping="Wrap" >  
  11.                 This is an Expander control. Within this control, all contents will be wrapped.  
  12.                 At run-time, you may expand or collapse this control. Type more text here to be typed.  
  13.                 Jump around and hype.   
  14.             </TextBlock>  
  15.         </Expander>  
  16.     </Grid>  
  17. </Window>  
The new output looks like this. As you can see from this figure, the header of the Expander control is different from the contents.


How to create an Expander control dynamically

The Expander class in WPF represents an Expander control.

This code snippet creates an Expander control at run-time.
  1. private void CreateDynamicExpander()  
  2. {  
  3.    Expander dynamicExpander = new Expander();  
  4.    dynamicExpander.Header = "Dynamic Expander";  
  5.    dynamicExpander.HorizontalAlignment = HorizontalAlignment.Left;  
  6.    dynamicExpander.Background = new SolidColorBrush(Colors.Lavender);  
  7.    dynamicExpander.Width = 250;  
  8.    dynamicExpander.IsExpanded = false;  
  9.    dynamicExpander.Content = "This is a dynamic expander";  
  10.   
  11.    RootGrid.Children.Add(dynamicExpander);  
  12. }  
How to set the direction of an Expander Control

The ExpandDirection property of the Expander control sets the direction of the Header. It can be Up, Down, Left or Right. The following code snippet sets the ExpandDirection to Up.
  1. <Expander Name="ExpanderControl" Background="LavenderBlush"   
  2.           HorizontalAlignment="Left" Header="Click to Expand"   
  3.           ExpandDirection="Up" IsExpanded="False" Width="200"  
  4.                   FontSize="20" FontWeight="Bold" Foreground="Green">  
  5.      <TextBlock TextWrapping="Wrap" >  
  6.          This is an Expander control. Within this control, all contents will be wrapped.  
  7.          At run-time, you may expand or collapse this control. Type more text here to be typed.  
  8.          Jump around and hype.   
  9.      </TextBlock>  
  10. </Expander>  
The control with Up direction looks like this.


How to set an image in an Expander Header

Expander.Header property may be used to style the header of an Expander control. Within the Header, you can set whatever contents you would like including an Image.

This code adds in image and text in the header of an Expander.
  1. <Expander Name="ExpanderControl"   
  2.   HorizontalAlignment="Left" Background="LavenderBlush"   
  3.   ExpandDirection="Down"  IsExpanded="False" Width="250"  
  4.           FontSize="20" FontWeight="Bold" Foreground="Green" >  
  5.     <Expander.Header>  
  6.         <BulletDecorator>  
  7.             <BulletDecorator.Bullet>  
  8.                 <Image Width="50" Source="Flowers.jpg"/>  
  9.             </BulletDecorator.Bullet>  
  10.             <TextBlock Margin="20,0,0,0">Flower Header</TextBlock>  
  11.         </BulletDecorator>  
  12.     </Expander.Header>  
  13.   
  14.     <TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Foreground="Black">  
  15.         This is an Expander control. Within this control, all contents will be wrapped.  
  16.         At run-time, you may expand or collapse this control. Type more text here to be typed.  
  17.         Jump around and hype.   
  18.     </TextBlock>  
  19. </Expander>  
The control with an image header looks like this.


How to add scrolling to an Expander Control

Adding a Scrollviewer control in the contents of an Expander adds scrolling to the Expander. This code adds scrolling to the contents of an Expander. 
  1. <Expander Name="ExpanderControl"   
  2.   HorizontalAlignment="Left" Background="LavenderBlush"   
  3.   ExpandDirection="Down"  IsExpanded="False" Width="250"  
  4.           FontSize="20" FontWeight="Bold" Foreground="Green" >  
  5.     <Expander.Header>  
  6.         <BulletDecorator>  
  7.             <BulletDecorator.Bullet>  
  8.                 <Image Width="50" Source="Flowers.jpg"/>  
  9.             </BulletDecorator.Bullet>  
  10.             <TextBlock Margin="20,0,0,0">Flower Header</TextBlock>  
  11.         </BulletDecorator>  
  12.     </Expander.Header>  
  13.     <Expander.Content>  
  14.         <ScrollViewer Height="100" VerticalAlignment="Top" >  
  15.             <TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Foreground="Black">  
  16.                 This is an Expander control. Within this control, all contents will be wrapped.  
  17.                 At run-time, you may expand or collapse this control. Type more text here to be typed.  
  18.                 Jump around and hype. This is an Expander control. Within this control, all contents will be wrapped.  
  19.                 At run-time, you may expand or collapse this control. Type more text here to be typed.  
  20.                 Jump around and hype.  
  21.             </TextBlock>  
  22.         </ScrollViewer>  
  23.     </Expander.Content>  
  24. </Expander>  
The Expander control with a scroll viewer looks like this.

 
 
Summary
 
In this article, we learned how to use an expander control in a WPF app.