typesafe NotifyPropertyChanged using linq expressions

What does the code that raises this look like? I’m guessing it is something like: NotifyOfPropertyChange(() => SomeVal); which is implicitly: NotifyOfPropertyChange(() => this.SomeVal); which does a capture of this, and pretty-much means that the expression tree must be constructed (with Expression.Constant) from scratch each time. And then you parse it each time. So the … Read more

BindableBase vs INotifyChanged

INotifyPropertyChanged The ViewModel should implement the INotifyPropertyChanged interface and should raise it whenever the propertychanges public class MyViewModel : INotifyPropertyChanged { private string _firstName; public event PropertyChangedEventHandler PropertyChanged; public string FirstName { get { return _firstName; } set { if (_firstName == value) return; _firstName = value; PropertyChanged(this, new PropertyChangedEventArgs(“FirstName”)); } } } } Problem … Read more

Subscribe to INotifyPropertyChanged for nested (child) objects

since I wasn’t able to find a ready-to-use solution, I’ve done a custom implementation based on Pieters (and Marks) suggestions (thanks!). Using the classes, you will be notified about any change in a deep object tree, this works for any INotifyPropertyChanged implementing Types and INotifyCollectionChanged* implementing collections (Obviously, I’m using the ObservableCollection for that). I … Read more

How to implement INotifyPropertyChanged in C# 6.0?

After incorporating the various changes, the code will look like this. I’ve highlighted with comments the parts that changed and how each one helps public class Data : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { //C# 6 null-safe operator. No need to check for event listeners //If there … Read more

Is [CallerMemberName] slow compared to alternatives when implementing INotifyPropertyChanged?

No, the use of [CallerMemberName] is not slower than the upper basic implementation. This is because, according to this MSDN page, Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time We can check that with any IL disassembler (like ILSpy) : the code for the “SET” operation of the … Read more

WPF MVVM INotifyPropertyChanged Implementation – Model or ViewModel

The thing is that if you were following MVVM, you would have a BookViewModel for your Book model class. So you would have a INotifyPropertyChanged implementation on that view model. Exactly for that purpose MVVM exists (but not only). That being said, the INotifyPropertyChanged has to be implemented on view model classes, not models. UPDATE: … Read more

How to get property change notifications with EF 4.x DbContext generator

I recently stumbled upon this problem, i edited my Entity.tt to implement the following changes, a quick patch but works great.. Add the following to the CodeStringGenerator class public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, “{0} {1}partial class {2}{3} : {4}”, Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(” : “, _typeMapper.GetTypeName(entity.BaseType)), “INotifyPropertyChanged”); } public string Property(EdmProperty edmProperty) … Read more

In MVVM model should the model implement INotifyPropertyChanged interface?

Implementing INotifyPropertyChanged in Models is totally acceptable – Typically, the model implements the facilities that make it easy to bind to the view. This usually means it supports property and collection changed notification through the INotifyPropertyChanged and INotifyCollectionChanged interfaces. Models classes that represent collections of objects typically derive from the ObservableCollection<T> class, which provides an … Read more

Automatically INotifyPropertyChanged

EDIT: The author of NotifyPropertyWeaver has deprecated the tool in favor of the more general Fody. (A migration guide for people moving from weaver to fody is available.) A very convenient tool I’ve used for my projects is Notify Property Weaver Fody. It installs itself as a build step in your projects and during compilation … Read more