views:

49

answers:

2

In a MVVM approach how would I go about binding to Properties.Settings? Is there a way to bind a property in C# code(in the ViewModel) to another property(Properties.Settings.Default) or should i just bind to standard properties and on save make sure each property gets propogated manually to the Properties.Settings?

+3  A: 

Something like the latter: expose settings in Properties.Settings.Default via properties on the ViewModel as appropriate for the view.

public class SomeViewModel
{
    public int SomeProperty
    {
        get
        { 
            return Properties.Settings.Default.SomeProperty; 
        }
        set
        { 
            Properties.Settings.Default.SomeProperty = value; 
        }
    }
}

...or code to that effect.

allonym
That is precisely the sort of boilerplate code which I try to avoid: You are adding nothing new. Rather to make this work correctly, you have to fire INPC when you change the value in the property. Additionally, you have to listen to property changes in the model (settings in this case), and also fire INPC when the underlying model changes. All this so you bind to ViewModel.Property instead of Settings.Property or ViewModel.Settings.Property?
Daniel Rose
+2  A: 

The settings implement INPC, so you could simply bind directly to the settings from your view. Remember that you manually have to call Save() on the settings to actually save them into isolate storage.

Daniel Rose
I wouldn't recommend binding directly to settings, actually, as it's not very MVVM-like. An exception might be if the view did nothing but allow for display/edit of things in settings, but even then I would be reluctant to consider Properties.Settings.Default a proper ViewModel.
allonym
MVVM is very subjective in some aspects. If the settings (which are basically (part of) the model) have the necessary properties, I see no problem directly binding to them. I also bind directly to my model for some properties which are already in the correct type. I see no use for boilerplate VM code which simply reimplements all model properties.
Daniel Rose
@Daniel - how do you go about testing that the view is actually getting the correct settings? You cannot because you have taken a shortcut past the viewmodel...
Peter Lillevold
@Peter Why should that be the case? Think of the settings as a second ViewModel.
Daniel Rose
You can always use the ViewModel to expose the 'Properties.Settings.Default' object, through a Property. Then bind the controls to the individual properties, this should give the necessary abstraction and also allow you to Command the Save() method.
Agies