1

I have 3 Expanders inside a StackPanel. Everything is working perfectly except that when I expand any of the expanders, the expander header is moved from the center of the StackPanel to the far left side. Once I collapse it, it goes back to the center. How can I get the expander header to remain centered?

Expander markup:

<Expander Header="My Header" Height="Auto" HorizontalAlignment="Center">
    ...
</Expander>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
B-Rad
  • 1,519
  • 5
  • 17
  • 32
  • this should not happen. can you post xaml code of your expander? – Bizhan Oct 11 '12 at 17:14
  • Pretty straight forward. Can't seem to see what is happening. – B-Rad Oct 11 '12 at 17:31
  • set Width to a fixed number like 250 – Bizhan Oct 11 '12 at 17:46
  • Thanks for the idea, however, that didn't change anything. – B-Rad Oct 11 '12 at 18:00
  • 1
    In fact, if I do set the Width then it doesn't get centered at all. – B-Rad Oct 11 '12 at 18:02
  • post the rest of your code. When collapsed, the expander will be a small size (probably relative to the size of just the header). When expanded, it will take into account the content, and if that content is set to stretch, the expander will stretch to accommodate that, iirc. Pictures of what you're talking about would help too. – John Gardner Oct 11 '12 at 18:17

1 Answers1

1

You need to create your own template for the Expander because it left-aligns header content.

You have the Expanders themselves centered, not the headers. When you expand, the content governs the width, so it stretches out to the width of the StackPanel and it becomes evident that the header is in fact left-aligned.

See How to align TexBlocks in WPF Expander Header for a treatment on templating the Expander.

Community
  • 1
  • 1
Jay
  • 56,361
  • 10
  • 99
  • 123