WPF DataBinding and Application Settings
Well, yet an other post on WPF and some DataBinding. But this time, this is about DataBinding to application settings that are automatically generated by visual studio. These settings come in handy when you want to save your application settings per user, or have some application wide settings. In my case, I wanted to have my application to remember its size and position, as well as the window state.
WinForms were providing an UI to do this, and I wanted to have all that functionality back. It is not all that "visual" as WinForms can do it, but it works rather well. I guess that Orcas will provide a way to do this visually.
All I had to do to use these settings from XAML was to create a resource to be usable for DataBinding, from a separate file :
[code:xml]File: SettingsRes.xaml
<ResourceDictionary xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:settings = "clr-namespace:WindowsApplication2.Properties">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<settings:Settings x:Key="settings" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
[/code]
WindowsApplication2 is the default namespace for the application, and since the Settings class is automatically generated, the default namespace is used. This resource will be used application wide and is referenced like this :
[code:xml]
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source = "SettingsRes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
[/code]
This is handy because resources are separated in multiple files. I also add here references to value converters, when I have to use them everywhere in the application. Value converters, yet an other interesting subject. Maybe in an other post :)
Now, about binding the data. We want to bind the Width, Height and WindowState of the default Window to some settings in the ApplicationSettings.
Here's what to do :
[code:xml]
<Window x:Class="WindowsApplication2.Window1"
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
Height = "{Binding Source={StaticResource settings}, Path=Default.MainWidth, Mode=TwoWay}"
Width = "{Binding Source={StaticResource settings}, Path=Default.MainHeight, Mode=TwoWay}"
WindowState = "{Binding Source={StaticResource settings}, Path=Default.MainState, Mode=TwoWay}">
[/code]
We create three bindings, each for an attribute, and set the source to the "settings" resource. The interesting part here is that the even thought the property we use is static, Settings.Default here, the Binding engine does seem to support it.
Setting the binding path to Default.MainWidth binds to the appropriate property. The last parameter instructs the binding to set the value of the property when the binding destination value changes, which can be the actual window height, for instance. The type of the property does not need to match the destination exact type, and since the type of the WindowState property is not known by VS2005 settings designer by default, setting the type to string seems to be enough.
Simple and easy. Now my application remembers where it was !
The sample here is a bit more complex, since it includes code to actually save the settings. But this is nothing really complex.
Man ! I like WPF... !