开发者

how to bind my view model properties to application settings?

My view model MyViewModel has a property MyProperty that i need to read/save from/to the application's settings. My project has Settings.settings file and the Settings type has MyProperty property. i need to bind MyViewModel.MyProperty to Settings.MyProperty so that any changes to MyViewModel.MyProperty are reflected in the Settings.MyProperty, and possibly the other way around. how can i do that?

note that i cannot derive MyViewModel from Settings because MyViewModel already derives another type开发者_运维技巧.

EDIT: i can do it manually of course: read and write from and to Settings in my property definition, but i am asking whether there is a more elegant approach.

class MyViewModel : ViewModelBase
{
    public int MyProperty { ... }
    public MyViewModel()
    {
        // here i need to bind Settings.Default.MyProperty to this.MyProperty
    }
}


rather than having the MyProperty on MyViewModel, you could treat the Settings object as another ViewModel (have it implement INotifyPropertyChanged). you can have more than one ViewModel. this would reduce duplication, obviously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜