Solved

StackPanel doesn't use all available space?

Posted on 2010-09-08
12
744 Views
Last Modified: 2013-11-12
Hi!

I've a stackpanel, that contains some Buttons.

Buttons have width set to "Stretch".

The stackPanel take correctly the whole space it can use, but all button within are only using something like prefered size or minimum size.

How can the whole space available for buttons are correctly spread between buttons?
<StackPanel Name="uxButtonPanel" Orientation="Horizontal" Background="Aqua">
        <Button/>
        <Button/>
        <Button/>
        <Button/>
        <Button/>
    </StackPanel>

Open in new window

0
Comment
Question by:Nargzul
12 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility

<Window x:Class="WpfApplication4.Window3"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window3" Height="300" Width="300">

    <Grid>

        <StackPanel>

            <Button MinHeight="25" Content="Help"/>

            <Button MinHeight="25" Content="Help"/>

            <Button MinHeight="25" Content="Help"/>

            <Button MinHeight="25" Content="Help"/>

        </StackPanel>

    </Grid>

</Window>

Open in new window

0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
but in your example, there is still 200px that aren't used, and I've some button that can have a "Collasped" visibility, and my window have to be resizeable, so definitely not usable in my case.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility

<Grid>

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition />

            <RowDefinition />

            <RowDefinition />

        </Grid.RowDefinitions>

        <Button Grid.Row="0" Content="Help"/>

        <Button Grid.Row="1" Content="Help"/>

        <Button Grid.Row="2" Content="Help"/>

        <Button Grid.Row="3" Content="Help"/>

    </Grid>

Open in new window

0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
yeah but this mean I've to create dynamically row definition every times I load buttons in this container/set a button as "Collapsed", this is really not efficient :S
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
Let's wait and watch if someone gives move efficient solution.
0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
Yeah... but this is already my current solution, so I need something better.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
You are using StackPanel and the last suggestion I've given is not to use StackPanel but use a Grid.
0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
Nope, I actually use a Grid, but I'm not actually satisfied with this solution, because I need to clear and reset the grid content too often.

So I was trying to do some things with the stack panel.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
using a dock panel would solve your problem to an extent. but the catch is that the last button will occupy the remaining space.

<DockPanel LastChildFIll = "true">
<Button c:Name="b1"/>
<Button c:Name="b2"/>
<Button c:Name="b3"/>
<Button c:Name="b4"/>
<Button c:Name="b5"/>
</<DockPanel>

in the above example if button b2 is removed button b5 will occupy it's spacer.

0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
Hum,, yeah but I can't have this behavior
0
 
LVL 8

Accepted Solution

by:
adler77 earned 500 total points
Comment Utility
Awhile back I cribbed part of the WpfContrib AutoGrid code and modified it to work like this. Sorry for the lack of comments--at the time, I was pretty much in "get it done" mode, but it isn't too complictated.

Inside the ArrangeChildren() override you will see a couple of places where I create a ColumnDefinition or a RowDefinition--you could adjust the properties there, or expand the class to accept more DPs from XAML. Either way, it might take a few minutes of adjusting to get it where you need it, but it should work.
namespace Manager
{
    public class AutoGrid : Grid
    {
        private int currentItemsCount;

        public int Items
        {
            get { return (int)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(int), typeof(AutoGrid),
            new PropertyMetadata(OnPropertyChanged));


        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(AutoGrid),
            new PropertyMetadata(OnPropertyChanged));

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AutoGrid grid = d as AutoGrid;
            if (grid != null)
            {
                grid.currentItemsCount = 0;
                grid.InvalidateArrange();
            }
        }

        public AutoGrid()
        {
            this.LayoutUpdated += new EventHandler(AutoGrid_LayoutUpdated);
        }

        void AutoGrid_LayoutUpdated(object sender, EventArgs e)
        {
            ArrangeChildren();
        }

        private void ArrangeChildren()
        {
            if (currentItemsCount != this.Children.Count)
            {
                currentItemsCount = this.Children.Count;

                this.ColumnDefinitions.Clear();
                this.RowDefinitions.Clear();

                bool horizontal = (this.Orientation == Orientation.Horizontal);

                for (int i = 0; i < Items; i++)
                {
                    if (horizontal)
                    {
                        ColumnDefinition cd = new ColumnDefinition();
                        this.ColumnDefinitions.Add(cd);
                    }
                    else
                    {
                        RowDefinition rd = new RowDefinition();
                        this.RowDefinitions.Add(rd);
                    }
                }

                if (Items != 0)
                {
                    int cells = (int)Math.Ceiling(this.Children.Count / (double)Items);
                    for (int i = 0; i < cells; i++)
                    {
                        if (!horizontal)
                        {
                            ColumnDefinition cd = new ColumnDefinition();
                            this.ColumnDefinitions.Add(cd);
                        }
                        else
                        {
                            RowDefinition rd = new RowDefinition();
                            this.RowDefinitions.Add(rd);
                        }
                    }
                }

                int x = -1;
                for (int i = 0; i < this.Children.Count; i++)
                {
                    FrameworkElement item = this.Children[i] as FrameworkElement;
                    if (item == null)
                    {
                        continue;
                    }

                    int position = i % this.Items;
                    if (position == 0)
                    {
                        x++;
                    }

                    if (horizontal)
                    {
                        Grid.SetColumn(item, position);
                        Grid.SetRow(item, x);
                    }
                    else
                    {
                        Grid.SetColumn(item, x);
                        Grid.SetRow(item, position);
                    }
                }
            }
        }
    }
}


<local:AutoGrid SizeChanged="AutoGrid_SizeChanged" Orientation="Horizontal" />

Open in new window

0
 
LVL 1

Author Closing Comment

by:Nargzul
Comment Utility
Wow, Thank you!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now