xsari3x
xsari3x

Reputation: 452

WPF make controls in dockpanel with same height same time filling all dock space?

My problem is very simple ,

I have a dock panel which i fill with buttons programmatically using this code

for(int i = 0 ; i< 5 ; i++)
{

Button btn = new Button();
btn.Content = "test";
btn.Height = Double.NaN;                  
DockPanel.SetDock(btn, Dock.Top);
dock1.Children.Add(btn);

}

Filling this dock

<DockPanel x:Name="dock1" Grid.Column="1" Background="LightSkyBlue"    >

If I set LastChildFill to False

this is the result :- enter image description here

If I set LastChildFill to True

this is the result :- enter image description here

What I want is these buttons to fill the dockpanel with same height and fill the area of the panel fully

Like this :-

enter image description here

Upvotes: 1

Views: 2475

Answers (1)

pushpraj
pushpraj

Reputation: 13669

you may perhaps use UniformGrid with 1 Column, this will hep you to distribute the space and size uniformly

example code

<UniformGrid Columns="1" Margin="10,0">
    <Button Height="25" Content="Motor gas 1:35 %"/>
    <Button Height="25" Content="Motor gas 2:36 %"/>
    <Button Height="25" Content="Motor gas 3:36 %"/>
    <Button Height="25" Content="Motor Air:200 %"/>
</UniformGrid>

result

result

here is code behind approach for the above example

xaml

<UniformGrid Columns="1" Margin="10,0" x:Name="uGrid1" Grid.Column="1" />

code

for(int i = 0 ; i< 5 ; i++)
{
    Button btn = new Button();
    btn.Content = "test";
    btn.Height = 25;
    uGrid1.Children.Add(btn);
}

Upvotes: 4

Related Questions