Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Be notified when visual/logical child added/removed

Tags:

.net

wpf

I am currently looking for a way to be notified when a child is added to the visual or logical children.

I am aware of the Visual::OnVisualChildrenChanged method, but it does not apply to me since I can't always inherit and override this function. I am looking for an event.

So, is there a way for the owner of a FrameworkElement/Visual to be notified when a child is added?

like image 996
decasteljau Avatar asked Oct 14 '08 13:10

decasteljau


2 Answers

Isn't it easier to extend

System.Windows.Controls.UIElementCollection 

to do the notification and use

protected override UIElementCollection CreateUIElementCollection(FrameworkElement logicalParent)

?

like image 62
Bram Avatar answered Oct 16 '22 18:10

Bram


I believe that FrameworkElement.Loaded and FrameworkElement.Unloaded are fired when the control is added to and removed from the Visual Tree, respectively. However, the few times that I was trying to do anything with them I wasn't able to get them to fire consistently (I was using Class Event Handlers at the time, so that might have something to do with it).

like image 24
Andy Avatar answered Oct 16 '22 19:10

Andy