wpfexpander

Right-align items within wpf expander header


I want to have some text in an expander header left aligned, then some text right aligned. I have found how to expand the header to the width of the container, and thought I could simply add a dockpanel and set the second text block to Dock Right, but it doesn't seem to help. Any solutions?

<Expander>
  <Expander.Header>
    <DockPanel
      Width="{Binding
        RelativeSource={RelativeSource
        Mode=FindAncestor,
        AncestorType={x:Type Expander}},
        Path=ActualWidth}">
      <TextBlock
        Text="I am header text…"
        Background="LightBlue"
      />
      <TextBlock DockPanel.Dock="Right"
        Text="I am header text…"
        Background="Yellow"
      />
    </DockPanel>
  </Expander.Header>
</Expander>

Solution

  • Try setting the TextAlignment property to right and the HorizontalAlignment property to stretch on the TextBlocks. That should help i think.

    If your use of color is for something other than demo purposes and you literally want the whole element to be right aligned then you might want to look at setting the LastChildFill property of the DockPanel to false.