Skip to content

WPF Persist UI State for VB.NET

March 11, 2010

First, I understand that VB.NET is the code behind and that technically WPF being the UI should mean that there is little/no difference in the XAML whether using C# or VB but I wasted time looking for this solution and finally figured the last little bit out on my own.

If you go into project properties (either Project -> Properties or right click on your project in the Solution Explorer and choose properties) one of the selections available is Settings. For a simple example of what you can do here, if you wanted to persist the full state of your window named ‘windowMain’ in windowMain.xaml, you could add the following to your settings list:

List of Settings

List of Settings

Then from your XAML you need to add the following to your window declaration:

Height="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainHeight,Mode=TwoWay}"
Width="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainWidth,Mode=TwoWay}"
Left="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainLeft,Mode=TwoWay}"
Top="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainTop,Mode=TwoWay}"
WindowState="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainState,Mode=TwoWay}"

Finally you need one line of code in your Window’s Closing event:


This has been written so many times by so many different people that I wouldn’t have bothered except for the fact that 100% of the examples I found were for C#. This is the equivalent of using the following in C# (I believe):

Height="{Binding Source={x:Static p:settings.Default}, Path=windowMainHeight,Mode=TwoWay}"
Width="{Binding Source={x:Static p:settings.Default}, Path=windowMainWidth,Mode=TwoWay}"
Left="{Binding Source={x:Static p:settings.Default}, Path=windowMainLeft,Mode=TwoWay}"
Top="{Binding Source={x:Static p:settings.Default}, Path=windowMainTop,Mode=TwoWay}"
WindowState="{Binding Source={x:Static p:settings.Default}, Path=windowMainState,Mode=TwoWay}"

Combining the TwoWay binding with the .Save() call in the Closing event means that however you move or change the state of the window, it will be saved and look the same when you reopen it. I have applied this same concept to all my grid column widths and various other UI elements so a user can truly customize and tweak the little things and have them stay that way.

One caveat is that this does not deal with the problems related to multiple monitors and the potential of restoring a window to a position that no longer exists. I have found what seems to be a good solution, with source code, for this issue if you are encountered with it here.

Also note this stores the settings by user on the machine the application runs on so changes made by one user will not affect another user on the machine, but the settings will not follow the user machine to machine.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: