# Advice for Improving WPF Performance ## Things to get rid of - Binding exceptions - Due to `RelativeSource` in `DataTemplates`. `RelativeSource` may break initially and raise an exception. Use inherited attached properties instead. - Due to nonexistent properties. This can easily happen if you’re binding to a mixed-type collection. Supply multiple `DataTemplates` instead. - Due to `RelativeSource.FindAnncestor`. `ItemsCollections` can cause items to be resolved before they are added to the visual tree. Push values down the tree instead using inherited attached properties. - Due to throwing converters - `DependencyPropertyDescriptor`, which can also cause memory leaks (framework issue) ## Things to try - Try to simplify the visual tree - Try to hard-code sizes (especially that of columns in `ItemControl`-s such as `ListView` and `DataGrid`) ## Things to batch - Collection update notifications - Use `ObservableCollection` but set it to null before individual updates. This should raise a property changed notification which causes WPF to stop listening to collection change notifications. - Or use a `BindingList` and suppress individual notifications - Or implement a custom `ObservableColleciton`. This needs to support: - `INotifyCollectionChanged`. Use Reset, not args constructors with collections, as they are generally not supported by CollectionViews. - Non-generic `IList` for random list access - `Dispatcher.BeginInvoke` calls ## Be careful with - `Behavior<T>.OnDetaching` as it often won’t be called. - `ViewModel` event subscriber references. ## Things to avoid - `CollectionView.Grouping` - `DynamicResources` - `ResourceDictionaries`. - They are very hard to follow. - Do try to collapse them as much as possible (ideally use only the top level dictionary). - Make sure dictionaries are not instantiated more than necessary. ## Sources - [Twelve Ways to Improve WPF Performance](https://www.evernote.com/shard/s182/nl/2147483647/202ed0fc-83c1-4d56-af35-6c17465bbe69/)