Save window size & position in WPF

* Right Click on Settings.settings and Open With XML (Text) Editor

* Add Top, Left, Height, Width & WindowState settings

  <Setting Name="Top" Type="System.Double" Scope="User">
    <Value Profile="(Default)">50</Value>
  <Setting Name="Left" Type="System.Double" Scope="User">
    <Value Profile="(Default)">50</Value>
  <Setting Name="Height" Type="System.Double" Scope="User">
    <Value Profile="(Default)">800</Value>
  <Setting Name="Width" Type="System.Double" Scope="User">
    <Value Profile="(Default)">1200</Value>
  <Setting Name="WindowState" Type="System.Windows.WindowState" Scope="User">
    <Value Profile="(Default)">Normal</Value>
* Add Settings property to App.xml
    <properties:Settings x:Key="Settings" />
* Add OnExit override to persist the settings when exiting application
protected override void OnExit(ExitEventArgs e)
* Add Top, Left, Height, Width & WindowState bindings to your MainWindow
<Window x:Class="TestApp.MainWindow"
  Top="{Binding Source={StaticResource Settings}, Path=Default.Top, Mode=TwoWay}"
  Left="{Binding Source={StaticResource Settings}, Path=Default.Left, Mode=TwoWay}"
  Width="{Binding Source={StaticResource Settings}, Path=Default.Width, Mode=TwoWay}"
  Height="{Binding Source={StaticResource Settings}, Path=Default.Height, Mode=TwoWay}"
  WindowState="{Binding Source={StaticResource Settings}, Path=Default.WindowState}">


Popular posts from this blog

Parse XML to dynamic object in C#

C# Updating GUI from different thread