locked
Change Window Location RRS feed

  • Question

  • Dears,

    In my application after the users changes the position of the window I want to get it to it's initial position from code.
    (WindowStartupLocation)

    any ideas????
    Sunday, January 20, 2008 6:59 AM

Answers

  • You can save the window's initial position values (Left and Top properties) inside the ContentRendered or Loaded event handlers when it first appeared. Later when you decided to restore the window back to its original position (ie: when LocationChanged event fired), just set these two properties back. WindowStartupLocation does not give you the position value of the window.

    Sunday, January 20, 2008 8:56 AM
  • Install a handler for the window's Loaded event and save the RestoreBounds property. 

     

    One great aspect of the RestoreBounds property is that it will give you a valid window rectangle even if the app starts up in a minimized state.  This is the same property that WPF programmers use when they want to start up the app in the same screen location the app occupied when it was last terminated.

    Sunday, January 20, 2008 12:07 PM

All replies

  • You can save the window's initial position values (Left and Top properties) inside the ContentRendered or Loaded event handlers when it first appeared. Later when you decided to restore the window back to its original position (ie: when LocationChanged event fired), just set these two properties back. WindowStartupLocation does not give you the position value of the window.

    Sunday, January 20, 2008 8:56 AM
  •  

    Dear thanx for your help I tried your method on test application and it worked as I need,

    but when I applied it on my application It failed, and here is the code of my window

     

    Code Block

    <Window

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="AUDWPF.MainWindow" WindowStyle="None" WindowState="Maximized"

    WindowStartupLocation="Manual" KeyDown="MainWindow_KeyDown" MouseDown="Page_mouseDown"

    Title="AUDWPF" Height="768" Width="1024" KeyUp="MainWindow_KeyUp" LocationChanged="loced" ResizeMode="NoResize" ContextMenuService.IsEnabled="False"

    x:Name="window" xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" >

     

     

     

     

    anyway thanx for trying to help I will mark ur post as an answer

    Sunday, January 20, 2008 11:59 AM
  • Install a handler for the window's Loaded event and save the RestoreBounds property. 

     

    One great aspect of the RestoreBounds property is that it will give you a valid window rectangle even if the app starts up in a minimized state.  This is the same property that WPF programmers use when they want to start up the app in the same screen location the app occupied when it was last terminated.

    Sunday, January 20, 2008 12:07 PM
  • Charlez,

    I guess am not understanding you well, cause i have tried this before and it's telling me that the RestoreBounds property is readonly, can u explain ur method for me,

     

    thanx alot

    Sunday, January 20, 2008 12:36 PM
  • You clearly said "I want to get it to it's initial position from code."  The way you get a window's location and size from code is by accessing the RestoreBounds property.

     

    If you want to set the window's location and size, you do so by setting the Left, Top, Width, and Height properties from the Rect you obtained from RestoreBounds.

     

     

     

     


    Sunday, January 20, 2008 12:59 PM
  • Charlez, sorry for the misunderstanding,

    thanx for trying to help

     

     

    • Proposed as answer by VeyQ Tuesday, April 16, 2013 2:21 PM
    Sunday, January 20, 2008 2:18 PM