Solved

StackPanel doesn't use all available space?

Posted on 2010-09-08
12
757 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

822 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