Skip to content

How to implement OnPropertyChanged

Simon Hughes edited this page May 16, 2019 · 8 revisions

In EF.Reverse.POCO.Core.ttinclude line 1082, change

from

Entity = string.Format(
    "public {0}{1} {2} {{ get; {3}set; }}{4}{5}",
    (OverrideModifier ? "override " : ""), WrapIfNullable(PropertyType, this), NameHumanCase, Settings.UsePrivateSetterForComputedColumns && IsComputed ? "private " : string.Empty, initialization, inlineComments
);

to

Entity = string.Format(
    "public {0}{1} {2} {{ get {{ return {2}; }} {3}set{{{2}=value;OnPropertyChanged(\"{2}\");}} }}{4}{5}",
    (OverrideModifier ? "override " : ""), WrapIfNullable(PropertyType, this), NameHumanCase, Settings.UsePrivateSetterForComputedColumns && IsComputed ? "private " : string.Empty, initialization, inlineComments
);

Changes to database.tt :

Settings.AdditionalNamespaces = new[] { "System.ComponentModel" };

// Writes optional base classes
Func<Table, string> WritePocoBaseClasses = t =>
{
    return ": INotifyPropertyChanged";
};


// Writes any boilerplate stuff inside the POCO class
Action<Table> WritePocoBaseClassBody = t =>
{
    WriteLine("        public event PropertyChangedEventHandler PropertyChanged;");
    WriteLine(@"        protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }");
};

Clone this wiki locally