none
Expander with custom header

    Question

  • Hi,

    i've spent a long time trying to figure this out, but with no luck. hopefully you of you guys can help.

    i'm want to use a Expander with the header content altered so that instead of just a TextBlock, I have a TextBlock plus a  CheckBox.
    This works fine, except that I'm unable to get the CheckBox to right align.


    I'm thinking that I might need to do this in code (calculating the widths and adjusting a grid etc.) but I'm hoping that there may be a simpler method.
    Any ideas?
    Thanks.
    Wednesday, March 18, 2009 1:13 PM

Answers

  • This is a bug with the Expander.

    Open the expander's control template and find the place where it has the ContentPresenter with HorizontalAlignment="Left".

     Change the HorizontalAlignment to bind to the templated parent's HorizontalContentAlignment property. I.e. replace "HorizontalAlignment="Left"" with  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"

    Now save your modified template and use that as your control template.



    • Proposed as answer by John J._ Monday, March 23, 2009 3:24 PM
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:24 AM
    Wednesday, March 18, 2009 2:16 PM
  • Changing the ControlTemplate as John j. describes worked for me.
    Thanks.
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:24 AM
    Monday, March 23, 2009 3:12 PM

All replies

  • <Expander> 
       <Expander.Header> 
          <CheckBox HorizontalAligment="Right">Yours text</CheckBox> 
       </Expander.Header> 
       .... 
       Expandable Content 
       .... 
    </Expander> 
      
    something like this?
    i didn't test it in VS..
    Wednesday, March 18, 2009 1:28 PM
  • I tried setting various combinations of HorizontalAlignment and HorizontalContentAlignment but none of them worked.
    Thanks though.

    It's strange that the mark-up below works (if I resize the window, the second column will remain at 40 units, with the first column resizing).
    Why can't I get this to work  inside the Expander header definition?


    <Window
    :
    :
    :

        <Grid Background="DarkGray">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="40"/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="SomeText" Grid.Column="0"/>
            <CheckBox Grid.Column="1"/>

        </Grid>
       
    </Window>

    Wednesday, March 18, 2009 1:42 PM
  • maybe becouse oryginal implementation of Expander host content in StackPanel with horizontal orientation...
    sth like this

    <Template>
         <StackPanel Orientation="Horizontal">
            <ToggleButton/> <!-- oryginal button for toggle IsExpanded-->
            <ContentPresenster Content={TemplatBinding Header}/> <!-- header content host -->
         </StackPanel>
    </Template>

    have u tried to override expander template?
    Wednesday, March 18, 2009 2:11 PM
  • This is a bug with the Expander.

    Open the expander's control template and find the place where it has the ContentPresenter with HorizontalAlignment="Left".

     Change the HorizontalAlignment to bind to the templated parent's HorizontalContentAlignment property. I.e. replace "HorizontalAlignment="Left"" with  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"

    Now save your modified template and use that as your control template.



    • Proposed as answer by John J._ Monday, March 23, 2009 3:24 PM
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:24 AM
    Wednesday, March 18, 2009 2:16 PM
  • John J._ said:

    Open the expander's control template and find the place where it has the ContentPresenter with HorizontalAlignment="Left".




    i have Blend in mind, right?
    Wednesday, March 18, 2009 2:23 PM
  • Something simple such as StyleSnooper will also work.
    Wednesday, March 18, 2009 2:48 PM
  • I'll try the suggestions later.
    Thanks for both your comments, they were very helpful.
    Wednesday, March 18, 2009 3:35 PM
  • Hi,
    Does it work?
    Monday, March 23, 2009 8:45 AM
  • Changing the ControlTemplate as John j. describes worked for me.
    Thanks.
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:24 AM
    Monday, March 23, 2009 3:12 PM
  • hi,

    I have Textblock and 2 buttons in expander header . Texblock should be left aligned and buttons should be right aligned. I am unable to get the buttons to right align.

    I tried as John J. described but didnt worked for me.

    Please reply.

    Thanks

    Sharath

    Thursday, April 09, 2009 8:35 AM
  • Is it possible to put the expander "button" on the opposite side?

    Matt
    Thursday, April 09, 2009 6:31 PM