is there a way to be notified when the visual parent of a FrameworkElement changes, without overriding the OnVisualParentChanged method, just by using events?
Here is the idea to use the "Loaded" event but this doesn't seem to be safe.
Is there an event that definitively fires when the the visual parent changes/changed?
This is somewhat to do with OnVisualParentChanged
. In the FrameworkElement.cs implementation, it does private void TryFireInitialized()
, which in turn will call protected virtual void OnInitialized(EventArgs e)
, so you could hook in there.
It does depend on what information you need to know. As far as I can tell, the only place to get notified that the parent is changing AND be able to access the old parent is OnVisualParentChanged (the old parent is passed in as the parameter). Otherwise any other place to hook in you will only be able to access the new parent, as it has already been changed.
You may also want to investigate some classes that inherit FrameworkElement and see if they expose any additional properties or methods that could help you.