Question

I was recently working with a lot of bindings to my configuration settings in XAML. Storing column widths/control sizes/window positions and the like. So I was wondering if there was an easy way to create and bind to "settings/configuration" values to XAML?

Right now I just create a setting in the project, shove a bindable property into the XAML's DataContext and go from there. But my settings count is getting pretty crazy and managing them is getting painful (boring, repetitive, and annoying).

In an ideal world I'd like a system where I can do something like this:

<Window State={Binding {Settings Name="MyWindowState", DefaultValue="Normal"}}/>

If the "MyWindowState" setting doesn't exist, it would be created automatically and stored somewhere. And if the MyWindowState setting changes, all the bindings that use it would also be notified and updated accordingly. And the DefaultValue would be used if the setting retrieval failed.

Does something akin to this exist already, or can it be achieved with the standard WPF XAML?

I am planning on working on something that can do this, but if a proven solution already exists I would love to at least look at it/hear it out.

From what I understand Telerik's persistance framework can do something like this, except on a control to control basis (there is no global "settings" I can bind to), at least on first glance.

No correct solution

OTHER TIPS

Yes, it's quite possible. If you have an application properties file, you can access it like this:

Height="{Binding MainWindowHeight, Mode=TwoWay, Source={x:Static p:Settings.Default}}"

where MainWindowHeight is a setting (in my case, an int). You'll also need to include this in the top of your XAML file, in the Window or UserControl tag:

xmlns:p="clr-namespace:APPLICATION_NAME.Properties"

where APPLICATION_NAME is the name of your application.

EDIT: The binding can be have any mode, I just use TwoWay so I don't have to have any actual code to update it. For the positioning of my windows, it works out nicely that way.

EDIT: Also, this can't dynamically create settings. I would use an XML file in your application, make a class to handle that, and then bind to a method of the class to get/dynamically create the values.

You can do this with an attached property:

<Window loc:WindowState.Name="MyWindowState" />

In the OnNameChanged event handler of your attached property you will have access to the Window instance that the WindowState.Name property was set on and access to the value ("MyWindowState" in this example) that was set. There you start listening (e.g. using PropertyChangedEventManager) to changes of all properties of your Window instance that are part of your window state that you want to persist.

May be you can use WPF's theming option. You can store settings of your controls (width, color...) in a theme file, which is nothing but an xml file. You may store this xml somewhere and load it at runtime. You can update this xml when the application exits with the changes. And load it when the application opens next.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top