Updating images in a wpf application
It keeps the observed object (generally, your view model) from accidentally holding a strong reference to your view. Viewmodels tend to have a longer lifetime than views (consider a virtualized Items Control), so it’s possible that your view model will inadvertently gather references to views.If your views or behaviors rely on events being raised from a viewmodel (as innocuous as INotify Property Changed. Use classes like Property Changed Event Manager or Collection Changed Event Manager, or (painfully) use the Weak Event Manager to create your own event manager for your custom events.This may not always be a practical or desirable solution, but layout passes perform faster when widths and heights do not have to be recalculated.They may also help stop a layout pass from rippling through an entire visual tree.If you have a lot of binding errors, then those split seconds start to add up.Make sure to go through your bindings, especially those on Items Controls (List Views, custom grids, etc.) and verify that there are no binding errors.There is no shortage of information out there on how to speed up the performance of WPF applications, but too often the focus is on the weird stuff instead of the simpler issues.
Blend-style behaviors also have their own set of lifetime problems.Open up your app in the debugger and play around, especially where there is slowness. Relative Source in Data Templates may also result in bindings that break initially, but then later resolve properly.Be wary of them, and try to use inherited attached properties instead of relying on Relative Source in Data Templates.Then there are some problems (like this and this) where WPF leaks for you too.Finally, there are things (this, this, this, this, this, and this) that simply perform worse than you likely expect.The easiest and surest way is to include it in the resources of your System. Application object, almost always difficult or infeasible for composite applications.I have also frequently taken to creating static classes that contain nothing but highly reusable resources (think static framework classes like the Brushes class) because it’s easier to guarantee that objects are only being created once, and hopefully at app startup instead of triggered by the user interacting with the application and forcing a lazy load at an undesirable time.Because of WPF’s references to your objects, it is not just enough to drop references to your view and view model.A better alternative is to rely on data binding where you can; create a Dependency Property for the sole purpose of listening to changes on your target property, and use the change notifications in Dependency Property in order to listen to changes on the target property.And always set specific widths on columns in a grid (be it a List View Grid View or any third-party control), because these tend to be very expensive, especially with larger data sets.Grouping in WPF doesn’t perform terribly well, especially with List Views and Grid Views.