Link to home
Start Free TrialLog in
Avatar of Nargzul
Nargzul

asked on

StackPanel doesn't use all available space?

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

Avatar of Gururaj Badam
Gururaj Badam
Flag of India image


<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

Avatar of Nargzul
Nargzul

ASKER

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.

<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

Avatar of Nargzul

ASKER

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
Let's wait and watch if someone gives move efficient solution.
Avatar of Nargzul

ASKER

Yeah... but this is already my current solution, so I need something better.
You are using StackPanel and the last suggestion I've given is not to use StackPanel but use a Grid.
Avatar of Nargzul

ASKER

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.
Avatar of Gautham Janardhan
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.

Avatar of Nargzul

ASKER

Hum,, yeah but I can't have this behavior
ASKER CERTIFIED SOLUTION
Avatar of adler77
adler77
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nargzul

ASKER

Wow, Thank you!