Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expander header has incorrect width with rotated text

Tags:

c#

wpf

xaml

I have an (right-direction) expander which should hide the header when expanded, and display some text rotated to be vertical down the right hand side when it is collapsed. I have managed to achieve this using the following code:

<Expander Grid.Column="1" ExpandDirection="Left" 
      IsExpanded="True" Name="rightHandExpander"
      MaxWidth="{Binding RelativeSource={RelativeSource PreviousData}}">
    <Expander.Header>
        <TextBlock Text="Header text" RenderTransformOrigin="0,0"
            Visibility="{Binding IsExpanded, RelativeSource={RelativeSource 
                         AncestorType={x:Type Expander}, Mode=FindAncestor}, 
                         Converter={StaticResource boolToVisibilityConverter}}">
            <TextBlock.LayoutTransform>
                <TransformGroup>
                    <RotateTransform CenterX="25" CenterY="25" Angle="90" />
                </TransformGroup>
            </TextBlock.LayoutTransform>
        </TextBlock>
    </Expander.Header>
    <!-- controls here -->
</Expander>

However when the expander is collapsed, the header takes up far too much space- the text is rotated, but the header's width appears to remain as if it has not.

Using VisualTreeHelper in the code-behind I discovered that the expander's header, border and dockpanel were too large, but the inner toggle button was the correct size. I tried setting the header, border and dockpanel's width to the correct size but they did not change.

Can anyone help?

Edit: The expander is contained in a grid with two columns with a grid splitter and another expander in the first column. The other expander does the same thing but collapsing to the left hand side- this expander seems to work correctly, however.

like image 309
jess Avatar asked May 31 '26 00:05

jess


1 Answers

I believe setting 'HorizontalAlignment' to 'Right' may fix the behaviour you are getting, as the default behaviour of most controls in a grid will be to fill all available space in their cell. It is kind of strange your left one works though, unless it is not directly in a grid, or has HorizontalAlignment set to Left?

like image 54
a little sheep Avatar answered Jun 01 '26 22:06

a little sheep