How do I keep buttons horizontally centered when resizing a window in WPF?

Hi:

I'm building a WPF application and having some difficulty in the layout of my buttons.

I have 3 command buttons. The buttons are a fixed height/width.
I want them fixed vertically (docked) to the bottom of my window.
When the window resizes vertically, the buttons are to remain at the bottom of the window.
(I've achieved this with a dock panel).

The buttons are to maintain a constant distance between themselves horizontally.
So, when the window resizes horizontally, I would like the 3 buttons centered (with the same distance between them).

I've tried experimenting with grids, stackpanels and dockpanels, but have not been quite able to figure this out.
How would I lay this out in XAML?

Thanks,
JohnB
LVL 1
jxbmaSoftware ConsultantAsked:
Who is Participating?
 
louisfrCommented:
You need a container around the buttons, with the HorizontalAlignment set to "Center".
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="461.436">
    <Grid >
        <Grid Height="300" Width="300" HorizontalAlignment="Center" Margin="0,10,0,10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" RenderTransformOrigin="-1.78,0.312"/>
            <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75" Grid.Column="2" RenderTransformOrigin="0.499,0.454"/>
        </Grid>
    </Grid>
</Window>

Open in new window

0
 
louisfrCommented:
If your buttons are in a Grid with 3 columns, stretched horizontally, they should keep their distance. Here's a quick example:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="461.436">
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" RenderTransformOrigin="-1.78,0.312"/>
        <Button Content="Button" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="75" Grid.Column="2" RenderTransformOrigin="0.499,0.454"/>
    </Grid>
</Window>

Open in new window

0
 
jxbmaSoftware ConsultantAuthor Commented:
::>louisfr - Thanks for the feedback.

Yes, this will keep the distance consistent between the buttons.

What I would really like to do is keep the "group of buttons" centered horizontally as the parent window/container changes size.

How do I do that?

Thanks,
JB
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.