为什么Properties.Settings.Default.PropertyChanged没有触发?

时间:2016-08-17 20:51:28

标签: c# wpf observablecollection settings

我在WPF MVVM应用程序的用户设置中有以下内容:

namespace MyApp.Properties {

    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public global::System.Collections.ObjectModel.ObservableCollection<MyApp.Models.MyItem> MyItems {
            get {
                return ((global::System.Collections.ObjectModel.ObservableCollection<MyApp.Models.MyItem>)(this["MyItems"]));
            }
            set {
                this["MyItems"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string TestSetting {
            get {
                return ((string)(this["TestSetting"]));
            }
            set {
                this["TestSetting"] = value;
            }
        }
    }
}

如果TestSetting中的值发生更改,则会触发事件Properties.Settings.Default.PropertyChanged,但当MyItem内的MyItems发生更改时,不会触发此事件。

如何解雇此事件?

1 个答案:

答案 0 :(得分:2)

分配给属性的对象必须在集合更改时触发自己的事件,其公共接口是INotifyCollectionChanged,已在ObservableCollection<T>中实现。您应该通过PropertyChanged收听属性的分配,然后订阅新分配的集合的CollectionChanged事件。