Friday, April 15, 2011

How to make WPF Expander Stretch?

The Expander control in WPF does not stretch to fill all the available space. Is there any solutions in XAML for this?

From stackoverflow
  • HorizontalAlignment="Stretch"
    
    Ngm : This is not working
  • All you need to do is this:

    <Expander>
      <Expander.Header>
        <TextBlock
          Text=”I am header text…”
          Background=”Blue”
          Width=”{Binding
            RelativeSource={RelativeSource
              Mode=FindAncestor,
              AncestorType={x:Type Expander}},
            Path=ActualWidth}”
          />
      </Expander.Header>
      <TextBlock Background=”Red”>
        I am some content…
      </TextBlock>
    </Expander>
    

    http://joshsmithonwpf.wordpress.com/2007/02/24/stretching-content-in-an-expander- header/

    Ngm : How do I do this in code? I realized that I have to account for some space for a button
    Jonathan Parker : Sorry I don't know how to do it in code.
    Josh G : TextBlock bx = new TextBlock(); bx.Text = "I am header text..."; Binding wdBind = new Binding("ActualWidth"); wdBind.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Expander), 1); bx.SetBinding(TextBlock.TextProperty, wdBind);
    Josh G : bx.Background = Colors.Blue; Expander ex = new Expander(); ex.Header = bx;
    Josh G : TextBlock contBx = new TextBlock(); contBx.Background = Colors.Red; contBx.Text = "I am some content..."; ex.Content = contBx;
    Josh G : ... This code is exactly equivalent to the XAML above.
    Jonathan Parker : Josh, you can always put this code in your own answer. That way it's more visible and can be voted on and marked as the answer.
  • Non stretchable Expanders is usually the problem of non stretchable parent controls.. Perhaps one of the parent controls has defined a HorizontalAlignment or VerticalAlignment property ?

    If you can post some sample code, we can give you a better answer..

    HTH

  • I Agree with HTH - check what sort of a container you're putting the Expander in... the StackPanel will always fold it's children down to the smallest size they can go to.

    I'm using Expanders a lot in my project, and if you drop them into a Grid / DockPanel, then the expander will fill all available space (assuming it's Vertical & Horizontal orientations are set to Stretch).

    Jonathan's suggestion of Binding the Expander's width to the container's width can get a bit tricky... I tried this technique a few weeks back and found that it can producte undesirable results in some cases, because it can inhibit the functioning of the layout system.

    PS: As a general tip (and I'm sure I'm gonna get flamed for writing this), if you're unsure of what sort of layout-container to your controls in, then start off with a Grid. Using the Column & Row definitions allows you to very easily control whether child controls use minimum space ("Auto"), maximum space ("*") or an exact amount of space ("[number]").

    Josh G : Grid is definitely the most versatile and easy to use container. I've heard that it performs much worse than most containers also.
    Bryan Anderson : @Mark, you mean you agree with Arcturus. HTH means Hope That Helps and is a common closing around here.
  • The Silverlight Toolkit includes an Accordion control which acts like an expander that always stretches to the available space. I haven't tested it yet, but it might be functional for WPF too, like the Silverlight Chart controls.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.