Search code examples
c#wpfxamlexpander

How to set Expander Direction to slide horizontally instead of vertically?


I know that the System.Windows.Controls.Expander is another "headered" content control in WPF just similar to a System.Windows.Controls.GroupBox. One advantage is that Expanders have the ability to hide its content by collapsing, and showing content by expanding.

My question is, what if I want my Expander to slide horizontally from left to right or right to left instead of vertically? and let's say I have the below Expander:

<StackPanel x:Name="RightPanel">
    <Expander x:Name="ExportExpander">
        <StackPanel>
            <TextBlock Name="x" Text="Element One"/>
            <Button Name="y" Content="Element Two"/>
        </StackPanel>
    </Expander>
</StackPanel> 

Solution

  • You could use ExpandDirection property of the Expander like this:

    <Expander x:Name="ExportExpander" ExpandDirection="Right">
    

    Also the correct code is like below:

    <StackPanel x:Name="RightPanel">
        <Expander x:Name="ExportExpander" ExpandDirection="Right">
            <StackPanel Orientation="Horizontal">
                <TextBlock Name="x" Text="Element One"/> <!--Textblock has Text property-->
                <Button Name="y" Content="Element Two"/>
            </StackPanel>
        </Expander>
    </StackPanel>