1

According to this answer:

  • When you implement INotifyPropertyChanged on a ViewModel, WPF binds to that ViewModel using a lightweight PropertyInfo object.
  • When you don't implement INPC, WPF creates heavier PropertyDescriptor objects for each property on the ViewModel and binds onto them instead, with the caveat that it can't detect changes made from within the ViewModel.

But what about common CLR types that are often bound to, but don't implement INPC, like string, int or T[]?

  1. Does WPF have some mechanism for detecting these types and binding them as special cases, or do they get the PropertyDescriptor treatment?
  2. If they are special-cased, is there somewhere a complete list of which types are treated that way and are "safe" to bind to without fear of the performance hit associated with PropertyDescriptor?

I'm specifically wondering about the types that feel to me to be "one tier up" from those above, like List<T>, Dictionary<TKey, TValue>, ValueTuple<...> and Exception.

Philip C
  • 1,819
  • 28
  • 50
  • `string`, `int` etc are most often the type of the **property** you bind to. The question however, whether or not a certain type implements `INotifyPropertyChanged` regards the type of the **DataContext** often a `ViewModel` that the bound properties belong to. So this likely already solves the question. The property type doesn't need to implement IPNC in order to let WPF realize its value has changed ... The DataContext is most often your own class where you can decide to implement IPNC if there are changes to be observed ... – lidqy Aug 26 '22 at 09:23
  • When it comes to collections, enumerables the interface that matters at least as much as IPNC is `INotifyCollectionChanged`. If you have a `List ` bound to e.g. a LisBox and you add an item in code, WPF will not notice, because the event of adding isn't fired by IPNC. You need INCC, most often using an ObservableCollection. Changes in a list are only fired as a change event if the list as a whole is replaced using the setter, eg: `dataContext.PersonList = new List()`. But not when elements are added, removed. Firing collection change events is the job of `INotifyCollectionChanged` – lidqy Aug 26 '22 at 09:29
  • @lidqy That makes sense - Paraphrasing your comments: WPF doesn't try to do any binding to an object until one of its properties is referenced from XAML. `string` and `int` don't have any properties so the question never arises. For collections too, no *property* is usually referenced in XAML (maybe `Count`?) - the collection is iterated over as a whole. I think you ought to make this an answer. – Philip C Aug 26 '22 at 13:12

0 Answers0