WPF positioning of control relative to parent

Posted on 2014-07-17
Last Modified: 2014-07-17
I have a window with a background image over which I need to position two buttons.

The window currently contains a grid - but it can be any panel as all I need are the buttons.

Because this will run on different screen sizes the background image will stretch to fill the screen so absolute positioning of the buttons is not going to work.

I am looking for something like the html

margin: 20% auto 0 auto; // 20% of screen height top margin and centering horizontally.

Is there something in XAML that natively supports this?

Currently I am writing code in the ContentRendered handler to get screen height and width and apply margins to the buttons with code.

Is this the only way?
Question by:Julian Hansen
    LVL 22

    Accepted Solution

    If it's already a grid, and you don't have many other controls in the grid (which could interfere), you can use a combination of button alignment and grid rowdefinitions.

    For the height in percentage, you can add two row definitions, one with height *, one with heigh 4* (equaling 20% and 80%).  Then set the buttons horizontal alignement to center, vertical alignment to top, and put it in the second row.

                <RowDefinition Height="*" />
                <RowDefinition Height="4*" />
            <Button HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="1" >Button Text</Button>

    Open in new window

    LVL 22

    Assisted Solution

    Just to cover the base of your question though, if your requirements were more complex, using ContentRendered is generally a last resort, especially just for positioning.

    Have a look at ValueConverters:

    What you would do in this case, is bind the button's margin to the parent's ActualHeight, and implement a custom ValueConverter, which would translate the height value passed in, into a Thickness value to be returned with the top as the calculated percentage.
    Note that it's important to use ActualHeight on the binding instead of Height.
    LVL 49

    Author Closing Comment

    by:Julian Hansen
    Thank you - both answers are equally useful as I will be using both in the project (not on the same window).

    Selection of accepted solution was arbitary and based purley on chronological order of the posts.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    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.…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now