Click here to Skip to main content
13,399,165 members (32,550 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 21 Nov 2010

How to Reposition a Silverlight Child Window?

, 21 Nov 2010
Rate this:
Please Sign up or sign in to vote.
How to Reposition a Silverlight Child Window?

Sometimes, we need to reset the position of the Silverlight Child Window at the center of the screen. This is generally required when you have a static instance of the ChildWindow and you want to use the same instance to show various messages in the screen. When you load the ChildWindow for the first time, by default it loads at the screen center. But think when your user repositions that ChildWindow to a different location in the screen, closes and reopens it, what will happen?

What you will see is, the ChildWindow opens at the same location where the user closed it the last time. So, how can we reset it to the center of the screen? This small tip will help you to achieve the functionality. Read it to know more details. The code is available for use.

The Silverlight ChildWindow internally implements its Template with a Grid named “ContentRoot”. If you edit your ChildWindow Template, you will see the following code:

<Grid x:Name="ContentRoot" 

      HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 

      VerticalAlignment="{TemplateBinding VerticalAlignment}" 

      Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"


You will see, it has a Grid named “ContentRoot”, which also has RenderTransform implemented in it. The TransformGroup of the Grid.RenderTransform contains various transformations of the Grid panel like ScaleTransform, SkewTransform, RotateTransform and TranslateTransform.

Here the TranslateTransform is used to position the ChildWindow. If you can set the X and Y coordinates of the TranslateTransform of the ChildWindow, you are done.

In this post, I will show you how to achieve that. To do this, we will create an Extension class for the ChildWindow and will implement the method to center it in the screen.

Let’s have a look into the implemented class here: 

/// <span class="code-SummaryComment"><summary>
</span>/// Extension class to do some extra operation with Silverlight ChildWindow.
/// <span class="code-SummaryComment"></summary>
</span>public static class ChildWindowExtensions
    /// <span class="code-SummaryComment"><summary>
</span>    /// Centers the Silverlight ChildWindow in screen.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="childWindow">The child window.
</span>    public static void CenterInScreen(this ChildWindow childWindow)
        var root = VisualTreeHelper.GetChild(childWindow, 0) as FrameworkElement;
        if (root == null) { return; }

        var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
        if (contentRoot == null) { return; }

        var group = contentRoot.RenderTransform as TransformGroup;
        if (group == null) { return; }

        TranslateTransform translateTransform = null;
        foreach (var transform in group.Children.OfType<translatetransform>())
            translateTransform = transform;

        if (translateTransform == null) { return; }

        // reset transform
        translateTransform.X = 0.0;
        translateTransform.Y = 0.0;

We created a static class having a static method “CenterInScreen()” which takes the instance of the ChildWindow as the parameter. From the instance of the ChildWindow, we will get the root which is nothing but a Grid. This Grid consists of another Grid named “ContentRoot”. Once you get the ContentRoot from the ChildWindow template, we can iterate through the TransformGroup to find the TranslateTransform. It is by default present in ChildWindow. Once you get the Translate Transform, change its X and Y coordinate values to ‘0’ (zero).

That’s all about it. If you want to position it somewhere else, set them there instead of ‘0’. Remember that, before doing this operation, i.e., before calling the CenterInScreen() your Child Window needs to be added in the layout. If you didn’t add the ChildWindow to the layout before calling the method, you will see the “ArgumentOutOfRangeException”:


The extension method will help you to call the CenterInScreen() directly from the ChildWindow instance. Hope this information will help you when you work with the same.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...

Comments and Discussions

QuestionMy Vote of 5 Pin
Member 94749396-Jun-13 23:42
memberMember 94749396-Jun-13 23:42 
GeneralMy vote of 5 Pin
folencao26-Jan-11 20:41
memberfolencao26-Jan-11 20:41 
GeneralMy vote of 5 Pin
Abhijit Jana23-Nov-10 18:44
mvpAbhijit Jana23-Nov-10 18:44 
GeneralRe: My vote of 5 Pin
Kunal_Chowdhury8-Jan-11 6:09
mvpKunal_Chowdhury8-Jan-11 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180218.2 | Last Updated 21 Nov 2010
Article Copyright 2010 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid