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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.