internal void OnContentChanged(AbstractProperty prop, object oldValue) { object content = Content; MPF.TryCleanupAndDispose(oldValue); if (!ReferenceEquals(oldValue, _convertedContent)) { MPF.TryCleanupAndDispose(_convertedContent); } object convertedContent; // Try to unwrap ResourceWrapper before _convertedContent is accessed elsewhere. // That's the only function we need from the ConvertType method, that's why we only call MPF.ConvertType // instead of TypeConverter.Convert. if (!MPF.ConvertType(content, typeof(object), out convertedContent)) { convertedContent = content; } _convertedContent = ReferenceEquals(content, convertedContent) ? MpfCopyManager.DeepCopySetLVPs(convertedContent, this, this) : convertedContent; if (ContentTemplate == null) { // No ContentTemplate set InstallAutomaticContentDataTemplate(); } FrameworkElement templateControl = _templateControl; if (!(_convertedContent is FrameworkElement) && templateControl != null) // If our content is a FrameworkElement itself, it should only be used as template control but not as context // The controls in the DataTemplate access their "data" via their data context, so we must assign it { templateControl.Context = _convertedContent; } }