Click here to monitor SSC

Bart

Software Engineer - Red Gate Software

WPF: How to get the bounds of a control in an automatic layout container, in the container coordinate space

Published Wednesday, November 24, 2010 1:41 PM

Googling this the other day, I started to get the impression that this might be annoyingly tricky. You might wonder why this is necessary at all, given that WPF implements layout for you in most cases (except for containers such as Canvas), but trust me, if you're developing custom elements, at some point you're probably going to need it.

If you're adding controls to a Canvas you can always use the attached Left, Right, etc., properties to get the bounds. Fortunately it's really no more difficult when using a container that implements automatic layout, such as StackPanel either. Before I go any further though, I should point out that the code in this post will not work for Silverlight. This is because the Silverlight VisualTreeHelper class doesn't implement the GetOffset method, which is available in the version of VisualTreeHelper that ships with the full .NET Framework from 3.5 onwards. Obviously this doesn't matter if you're never going to deploy to Silverlight, but if you're developing controls that might be used in both WPF and Silverlight apps, you might want to look for an alternative solution.

Anyway, here's the C# code for WPF:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Reflector.Application.Util
{
  
static class ControlBoundsHelper
   {
      
public static Rect GetBoundsInParentCoordinateSpace( Control control )
      
{
           Vector  offset 
= VisualTreeHelper.GetOffset( control );
          
return new Rect( offset.X, offset.Y, control.ActualWidth, control.ActualHeight );
      
}
   }
}

Pretty simple, right?

The one gotcha is that if you call this immediately after adding a control to its parent, before the layout has been updated, you'll get (0, 0, 0, 0) in the Rect. To fix this you need to force a layout update by calling UpdateLayout on the parent control.

Until next time.

by Bart Read
Filed Under:

Comments

No Comments
You need to sign in to comment on this blog

About Bart Read

Bart has done many things since he started work at Red Gate Software Ltd in August 2004, but nowadays he's (mainly) the product manager for the .NET Developer Tools. He still feels like this is a bit like admitting you were cheering for the Empire whilst watching Star Wars, but for now he's along for the ride. In a previous incarnation he was a project manager leading the .NET Reflector Pro, ANTS Memory Profiler 5, ANTS Performance Profiler 4 & 5, and SQL Prompt 3.0 - 3.6 projects. He still occasionally writes some code and, in the past, has touched the code for most of the Red Gate SQL developer tools... some of them still haven't recovered from the shock. He was born and grew up in Dorset, was educated in Nottingham and London, and likes music and real ale. His photo is extremely misleading.
Latest articles
Backups, What Are They Good For?
 We've heard the confessional story from Pixar that Toy Story 2 was almost lost due to a bad backup, but... Read more...

C# Async: What is it, and how does it work?
 The biggest new feature in C#5 is Async, and its associated Await (contextual) keyword. Anybody who is... Read more...

Handling Deadlocks in SQL Server
 In this excerpt from his book Troubleshooting SQL Server: A Guide for the Accidental DBA, Jonathan... Read more...

SQL VIEW Basics
 SQL Views are essential for the database developer. However, it is common to see them misued, or... Read more...

The PoSh DBA: Grown-Up PowerShell Functions
 Laerte goes step-by-step through the process of tidying up and making more reusable an untidy... Read more...