Clear "preferences" binding in WPF

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

Now I just create a parameter in the project, inserting the bindable property into the XAML DataContext and from there. But my account of settings gets pretty crazy, and managing them becomes painful (boring, repetitive and annoying).

In an ideal world, I need a system where I can do something like this:

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

If the "MyWindowState" parameter does not exist, it will be created automatically and stored somewhere. And if the MyWindowState parameter changes, all the bindings that use it will also be notified and updated accordingly. And DefaultValue will be used if the search for settings failed.

Is there something similar to this already or can be achieved using standard WPF XAML?

I plan to work on something that can do this, but if a proven solution already exists, I would like to at least look at it / hear it.

From what I understand Framework Telerik persistance , you can do something like this, with the exception of the control for control (there are no global "settings" with which I can bind), at least at first glance.

+7
c # wpf xaml
source share
3 answers

You can do this with an attached property:

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

In the OnNameChanged event handler of your connected property, you will gain access to the Window instance for which the WindowState.Name property is set and access to the value ("MyWindowState" in this example) that was set. There, you start listening (for example, using the PropertyChangedEventManager ) to change all the properties of your Window instance that are part of your window state that you want to save.

0
source share

Perhaps you can use the WPF theming option. You can save the settings of your controls (width, color ...) in the theme file, which is just an XML file. You can store this xml somewhere and load it at runtime. You can update this xml when the application exits with the changes. Download it when the application opens further.

0
source share

Yes, it is quite possible. If you have an application properties file, you can access it as follows:

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

where MainWindowHeight is a parameter (in my case, int). You also need to include this at 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 in any mode, I just use TwoWay, so I don't need to have any actual code to update it. This works just fine for positioning my windows.

EDIT: Also, it cannot dynamically create settings. I would like to use an XML file in your application, create a class to process it, and then bind to a class method to get / dynamically create values.

0
source share

All Articles