【问题标题】:how to bind my view model properties to application settings?如何将我的视图模型属性绑定到应用程序设置?
【发布时间】:2011-04-28 04:35:25
【问题描述】:

我的视图模型 MyViewModel 有一个属性 MyProperty,我需要从应用程序的设置读取/保存它。我的项目有 Settings.settings 文件,并且 Settings 类型有 MyProperty 属性。我需要将 MyViewModel.MyProperty 绑定到 Settings.MyProperty,以便对 MyViewModel.MyProperty 的任何更改都反映在 Settings.MyProperty 中,反之亦然。我该怎么做?

请注意,我无法从 Settings 派生 MyViewModel,因为 MyViewModel 已经派生了另一种类型。

编辑:我当然可以手动完成:在我的属性定义中读取和写入设置,但我想问是否有更优雅的方法。

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

【问题讨论】:

    标签: .net mvvm settings binding


    【解决方案1】:

    您可以将 Settings 对象视为另一个 ViewModel(让它实现 INotifyPropertyChanged),而不是在 MyViewModel 上使用 MyProperty。您可以拥有多个 ViewModel。这显然会减少重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2010-12-02
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      相关资源
      最近更新 更多