tags:

views:

2483

answers:

8

I need to be able to fill a stackpanel with buttons but the buttons must appear at the bottom of the stackpanel first and populate upwards. The buttons are created dynamically and there's an unknown number of them so visual hackery just won't work. I've tried experimenting with vertical alignments but to no avail.

Thanks in advance.

+3  A: 

The best way to solve the problem is to implement custom container derived from stackpanel but quick and dirty solution if elements are added at runtime is

    public Window1()
    {
        InitializeComponent();
        for (int i = 0; i < 10; i++)
        {
            Button btn = new Button();
            btn.Content = "Button " + i;
            MyStack.Children.Insert(0, btn);
        }
    }

Just insert item at 0 position instead of adding them.

Oleg
A: 

Try putting the StackPanel inside another container (not a StackPanel; maybe a DockPanel) and bottom-aligning it. Then when you populate the buttons, put each new one into the first position.

Ria
+10  A: 

Like so:

<StackPanel VerticalAlignment="Bottom">
    ...
</StackPanel>

and to populate with buttons upward you must insert the buttons at position 0, instead of adding them.

Pop Catalin
Populating by insertion of course only works if you add the elements manually in code. I developed (with some help) a ReversibleStackPanel here that may be of use to folks: http://stackoverflow.com/questions/3428622/building-a-reversible-stackpanel-in-wpf
chaiguy
A: 

Thanks guys. Still lot's to learn with WPF!

Stimul8d
+1  A: 

Another alternative is to use a DockPanel instead.

Just set the LastChildFill to false on the DockPanel.

Then set the attached Dock property to each button you are adding to Bottom before adding to the DockPanel.

example :

            var button = new Button();
            DockPanel.SetDock(button, Dock.Bottom);
JB
LastChildFill! I wish I knew about that before! Why they didn't just have a Dock.Fill I'll never know. That whole taking the last child as the fill is ridiculous.
chaiguy
+1  A: 

I found that using a UniformGrid with Column=1 gives a neat filling stack, or set Rows=1 give a neat horizonatally filled stack. And adding from the index 0 will work from bottom up.

Tri Q
A caveat of using the `UniformGrid` is each element added will have the same horizontal/vertical sizing, so if the elements themselves are different sizes, they won't touch when stacked like with a `StackPanel`.
chaiguy
+1  A: 

Or you can rotate the StackPanel 180 degrees to get the buttons to stack from the bottom to the top and then rotating the buttons another 180 degrees to get them right-side-up again:

<StackPanel>
    <!-- rotate all buttons inside this panel -->
    <StackPanel.Resources>
        <Style TargetType="Button">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="180"/>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <!-- rotate the stack panel -->
    <StackPanel.LayoutTransform>
       <RotateTransform Angle="180"/>
    </StackPanel.LayoutTransform>

    <!-- content -->
    <Button>1</Button>
    <Button>2</Button>

</StackPanel>
Nir
Clever, but probably pretty cpu-intensive.
chaiguy
@chaiguy - I think you are wrong about this being cpu intensive because: 1. transforms are extremely efficient and 2. they run only when recalculating layout - I'll be very surprised if this has any noticeable impact on performance (or cpu usage)
Nir
@Nir I could very well be wrong in this case, but I do remember seeing a MS-published article on improving performance in WPF and one of the points was to avoid using LayoutTransforms excessively--primarily because they cause additional layout passes, but as you say that only happens when it changes so it may not be a big deal.
chaiguy
@chaiguy - you are right, LayoutTransform does cause a layout pass - but only when it changes - the performance problem they wrote about is animating layout transforms (having 30 layout passes per second is, obviously, very cpu intensive). in this specific example the transform will never cause extra layout passes because it never changes.
Nir
A: 

Love the transforms solution by Nir. I was wondering if it could be done using transforms.

One caveat, though: Don't use the transforms trick on a ScrollView based control such as a ListBox because the scroll bar operation will be inverted from the content. Hilarious to watch, as long as you're not the end user. ;>

dthorpe
lol :) ........
chaiguy