WPF FAQ - Expander

How can I bind the Expander’s header element’s Width to the ActualWidth of the Expander ?

This can be done with the following code snippet.

[XAML]
<Expander>
            <Expander.Header>
                <TextBlock Text="Expander header content" Background="AliceBlue"
                           Width="{Binding
                           RelativeSource={RelativeSource
                           Mode=FindAncestor,
                           AncestorType={x:Type Expander}},
                           Path=ActualWidth}"/>
            </Expander.Header>
  
            <TextBlock Background="Gray" Text="Expander body content"/>
             
</Expander>
Permalink
How do I make the Expander button to be justified to the right, rather than the left which is the default ?

If you require justifying the Expander button to the right, you need to set the FlowDirection property to RightToLeft.

Note: If you set the FlowDirection to RightToLeft, the content of the expander will also be moved to RightToLeft justification. In order to avoid this, you can set the flow direction to expander content. This can be done with the following code snippets,

[XAML]

<Expander FlowDirection="RightToLeft">
            <Expander.Header>
                <TextBlock Text="Expander header content" Background="AliceBlue"
                           Width="{Binding
                           RelativeSource={RelativeSource
                           Mode=FindAncestor,
                           AncestorType={x:Type Expander}},
                           Path=ActualWidth}"/>
            </Expander.Header>
            <Grid FlowDirection="LeftToRight">
                <TextBlock Background="Gray" Text="Expander body content"/>
            </Grid>
</Expander>
Permalink
How can I create an Expander with a ScrollViewer ?

This example shows how to create an Expander control that contains complex content such as an image and text. The example also encloses the content of the Expander in a ’ScrollViewer’ control.

[XAML]

<Expander Width='200' HorizontalContentAlignment='Stretch'>
   <Expander.Header>
     <BulletDecorator>
       <BulletDecorator.Bullet>
         <Image Width='10' Source='images\icon.jpg'/>
       </BulletDecorator.Bullet>
       <TextBlock Margin='20,0,0,0'>My Expander</TextBlock>
     </BulletDecorator>
   </Expander.Header>
   <Expander.Content>
     <ScrollViewer Height='50'>
       <TextBlock TextWrapping='Wrap'>
         Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
         sed do eiusmod tempor incididunt ut labore et dolore magna 
         aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
         ullamco laboris nisi ut aliquip ex ea commodo consequat. 
         Duis aute irure dolor in reprehenderit in voluptate velit 
         esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 
         occaecat cupidatat non proident, sunt in culpa qui officia 
         deserunt mollit anim id est laborum.
       </TextBlock>
     </ScrollViewer>
   </Expander.Content>
 </Expander>
Permalink