How to put Expander ToggleButton on right
Asked Answered
E

2

9

By default the expander has a left aligned toggle button but in my WPF app i want toggle button on the right side of the header without the help of Expression Blend. just plain XAML and/or C#. My expander contains a vertically oriented stackpanel which has labels as its child.

I went for its part but here it says "The Expander control does not have any named parts".

I found an example here. But it overrides the default Expander Style.

I think the attached image should convey what i want. How to do. Any link would be helpful.

enter image description here

Elkin answered 15/2, 2012 at 5:33 Comment(0)
P
11

Use this:

<Expander Header="Expander1" FlowDirection="RightToLeft">
    <TextBlock FlowDirection="LeftToRight">
    </TextBlock>
</Expander>

Add your content in the TextBlock, if you don't want to the whole content to be right to left.

Photocathode answered 15/2, 2012 at 6:9 Comment(3)
@NikhilAgrawal As I said, you should put your content inside a LeftToRight container (TextBlock in my example). You can set FlowDirection of your StackPanel to LeftToRight.Photocathode
I did what u suggested. My expander contains a vertically oriented stackpanel (Orientation = Vertical) which has labels as its child (Added Dynamically). I set the expander FlowDirection="RightToLeft" and stackpanel FlowDirection="LeftToRight" and it worked. ThanksElkin
This will align the header to the right of the window.Hesperidin
E
18

There is a trick that can help

<Expander Header="My Expander" 
          FlowDirection="RightToLeft">
    <Expander.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Expander}, Path=Header}"
                       Width="{Binding RelativeSource={RelativeSource AncestorType=Expander}, Path=ActualWidth}" 
                       Margin="-30,0,0,0"
                       FlowDirection="LeftToRight">
            </TextBlock>
        </DataTemplate>
    </Expander.HeaderTemplate>
</Expander>
Edaphic answered 25/7, 2013 at 6:10 Comment(3)
Can you please explaing how your answer is different from MD.Unicorn's answer?Elkin
Yes. With his answer the Header text will be right aligned as well along with the ToggleButton. In my solution Header text is still left aligned and just ToggleButton is shifted from Left to Right.Edaphic
Though the questioner didn't expect this answer or probably didn't really need this, it helped me out with my own problem. Cheers!Brick
P
11

Use this:

<Expander Header="Expander1" FlowDirection="RightToLeft">
    <TextBlock FlowDirection="LeftToRight">
    </TextBlock>
</Expander>

Add your content in the TextBlock, if you don't want to the whole content to be right to left.

Photocathode answered 15/2, 2012 at 6:9 Comment(3)
@NikhilAgrawal As I said, you should put your content inside a LeftToRight container (TextBlock in my example). You can set FlowDirection of your StackPanel to LeftToRight.Photocathode
I did what u suggested. My expander contains a vertically oriented stackpanel (Orientation = Vertical) which has labels as its child (Added Dynamically). I set the expander FlowDirection="RightToLeft" and stackpanel FlowDirection="LeftToRight" and it worked. ThanksElkin
This will align the header to the right of the window.Hesperidin

© 2022 - 2024 — McMap. All rights reserved.