none
Problem: how to centre a page on the screen (not hosted in a browser)

    Question

  • with a page based application (not hosted in a browser),  how can you centre a page, or position a page?. There is no control over where the page displays if it is smaller than the screen. With a window you can use something like whats shown below. But a page has no Top property you can set:

    win.Top = (My.Computer.Screen.Bounds.Height - win.Height) / 2

    win.Left = (My.Computer.Screen.Bounds.Width - win.Width) / 2





    • Edited by carrera7 Monday, January 05, 2009 3:30 AM update
    Monday, January 05, 2009 3:29 AM

Answers

  • Hello Carrera7,

      In addition to other's,

      There several ways to get the host window.

      Here is an example: From the Parent add the following code to the page loaded event.

     private void Page_Loaded(object sender, RoutedEventArgs e)  
            {  
                NavigationWindow winhost = this.Parent as NavigationWindow;  
     
                System.Drawing.Rectangle rec = System.Windows.Forms.Screen.PrimaryScreen.Bounds;  
     
                winhost.Top = (rec.Height - winhost.ActualHeight) / 2;  
                winhost.Left = (rec.Width - winhost.ActualWidth) / 2;  
            } 


    Please notice: it is the C# code, Screen Class is from Windows.Forms namespace.

    It should be in the pixel format. winhost.ActualHeight is a device-independent length.

    We need a more precise calculation.

    Thanks.
     


    Please mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by carrera7 Friday, January 09, 2009 1:50 AM
    Wednesday, January 07, 2009 1:38 PM
  • Since you're not browser hosted, you are inside a window of some sort.  Instead of using a solution that will be broken by changing DPI of screen, try just setting the WindowStartupLocation property on the host window...

    <Window x:Class="WindowsFormsApplicationShowingWPF.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        ...  
        WindowStartupLocation="CenterScreen"> ...  

    HTH,
    Matt
    SDET : Deployment/Hosting
    Wednesday, January 07, 2009 8:43 PM
    Moderator

All replies

  • You have to set the properties on the window, because a page is hosted within this window.
    But if you have just one window, have you tried to use App.Current.MainWindow?
    Monday, January 05, 2009 8:29 AM
  • How about setting the HorizontalAlignment/VerticalAlignment properties of the Page to 'Center'?

    hth,
    Marcel
    Monday, January 05, 2009 9:00 AM
  •  @DutchMarcel

    No progress here.

    Everytime you load the page it can be in a different position which is frustrating.
    Monday, January 05, 2009 3:25 PM
  • Hello Carrera7,

      In addition to other's,

      There several ways to get the host window.

      Here is an example: From the Parent add the following code to the page loaded event.

     private void Page_Loaded(object sender, RoutedEventArgs e)  
            {  
                NavigationWindow winhost = this.Parent as NavigationWindow;  
     
                System.Drawing.Rectangle rec = System.Windows.Forms.Screen.PrimaryScreen.Bounds;  
     
                winhost.Top = (rec.Height - winhost.ActualHeight) / 2;  
                winhost.Left = (rec.Width - winhost.ActualWidth) / 2;  
            } 


    Please notice: it is the C# code, Screen Class is from Windows.Forms namespace.

    It should be in the pixel format. winhost.ActualHeight is a device-independent length.

    We need a more precise calculation.

    Thanks.
     


    Please mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by carrera7 Friday, January 09, 2009 1:50 AM
    Wednesday, January 07, 2009 1:38 PM
  • Since you're not browser hosted, you are inside a window of some sort.  Instead of using a solution that will be broken by changing DPI of screen, try just setting the WindowStartupLocation property on the host window...

    <Window x:Class="WindowsFormsApplicationShowingWPF.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        ...  
        WindowStartupLocation="CenterScreen"> ...  

    HTH,
    Matt
    SDET : Deployment/Hosting
    Wednesday, January 07, 2009 8:43 PM
    Moderator