A while ago, I was reading this shocking blog post by Joost van Schaik. It explains that the Behavior.OnDetaching is not always invoked and thus can cause (big) memory issues. He gives a great solution and shows a save way to handle this. He also provides a code snippet to implement this behavior more easily. However, I don’t like writing code twice, even when it is being generated. Therefore, I have decided to continue is excellent work and write a BehaviorBase that completely handles all of this out of the box. In the end, this is what I came up with: /// <summary>
/// Behavior base class that handles a safe unsubscribe and clean up because the default
/// behavior class does not always call <see cref="Behavior.OnDetaching"/>.
/// <para />
/// This class also adds some specific features such as <see cref="ValidateRequiredProperties"/>
/// which is automatically called when the behavior is attached.
/// </summary>
/// <typeparam name="T">The dependency object this behavior should attach to.</typeparam>
public abstract class BehaviorBase<T> : Behavior<T> where T : FrameworkElement
{
/// Behavior base class that handles a safe unsubscribe and clean up because the default
/// behavior class does not always call <see cref="Behavior.OnDetaching"/>.
/// <para />
/// This class also adds some specific features such as <see cref="ValidateRequiredProperties"/>
/// which is automatically called when the behavior is attached.
/// </summary>
/// <typeparam name="T">The dependency object this behavior should attach to.</typeparam>
public abstract class BehaviorBase<T> : Behavior<T> where T : FrameworkElement
{
#region Variablesprivate bool _isClean = true;#endregion#region Constructor & destructor
#endregion#region Properties
#endregion#region Properties