Solved

StackPanel doesn't use all available space?

Posted on 2010-09-08
12
750 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
ID: 33625455

<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
ID: 33625491
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
ID: 33625814

<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
ID: 33625832
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
ID: 33625851
Let's wait and watch if someone gives move efficient solution.
0
 
LVL 1

Author Comment

by:Nargzul
ID: 33625865
Yeah... but this is already my current solution, so I need something better.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33625876
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
ID: 33626214
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
ID: 33649583
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
ID: 33659757
Hum,, yeah but I can't have this behavior
0
 
LVL 8

Accepted Solution

by:
adler77 earned 500 total points
ID: 33667631
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
ID: 33669460
Wow, Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

11 Experts available now in Live!

Get 1:1 Help Now