Learn how to a build a cloud-first strategyRegister Now


WPF positioning of control relative to parent

Posted on 2014-07-17
Medium Priority
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
  • 2
LVL 23

Accepted Solution

Snarf0001 earned 2000 total points
ID: 40201829
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 23

Assisted Solution

Snarf0001 earned 2000 total points
ID: 40201861
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 60

Author Closing Comment

by:Julian Hansen
ID: 40201908
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

810 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