{"id":475,"date":"2010-02-16T23:48:22","date_gmt":"2010-02-16T21:48:22","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=475"},"modified":"2010-02-16T23:48:22","modified_gmt":"2010-02-16T21:48:22","slug":"inotifypropertychanged-with-lambdas","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/inotifypropertychanged-with-lambdas","title":{"rendered":"INotifyPropertyChanged with lambdas"},"content":{"rendered":"<p>There are several ways of implementing <em>INotifyPropertyChanged<\/em> in WPF applications.<\/p>\n<p>One that I particularly like is by using <a href=\"\/blog\/inotifypropertychanged-with-postsharp\">PostSharp to implement INotifyPropertyChanged<\/a>.<\/p>\n<p>If you are not comfortable with PostSharp you can at least get rid of those nasty strings in standard implementation:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Model : ViewModeBase\n{\n    private string _status;\n    public string Status\n    {\n        get { return _status; }\n        set\n        {\n            _status = value;\n            OnPropertyChanged(&quot;Status&quot;);\n        }\n    }\n};\n<\/pre>\n<p>Labda expressions look nicer and are easier to refactor:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class MyModel : ViewModeBase\n{\n    private string _status;\n    public string Status\n    {\n        get { return _status; }\n        set\n        {\n            _status = value;\n            OnPropertyChanged(() =&gt; Status);\n        }\n    }\n};\n<\/pre>\n<p>First thing we need to do is to create base class for all our ViewModels:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class ViewModelBase\n{\n    public event PropertyChangedEventHandler PropertyChanged\n        = delegate { };\n\n    protected void OnPropertyChanged(\n        Expression&lt;func&lt;object&gt;&gt; expression)\n    {\n        string propertyName = PropertyName.For(expression);\n        this.PropertyChanged(\n            this,\n            new PropertyChangedEventArgs(propertyName));\n    }\n};\n<\/pre>\n<p>The implementation of PropertyName.For is very straightforward: <a href=\"\/blog\/property-name-from-lambda\">How to get property name from lambda<\/a>.<\/p>\n<p>That&#8217;s it!<br \/>\nNice, easy to refactor, compile-time checked <em>INotifyPropertyChanged<\/em> implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways of implementing INotifyPropertyChanged in WPF applications. One that I particularly like is by using PostSharp to implement INotifyPropertyChanged. If you are not comfortable with PostSharp you can at least get rid of those nasty strings in standard implementation: public class Model : ViewModeBase { private string _status; public string Status { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[37,62],"class_list":["post-475","post","type-post","status-publish","format-standard","hentry","category-programming","tag-mvvm","tag-wpf"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/475"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}