WPF event to capture new added controls?

We use ContentControl and other containers in WPF. I need a notification with a new child control to be added to the container. What is the best way to get a newly created control inside a parent?

+4
source share
2 answers

ContentControl contains only one child, which is connected through the ContentControl.Content property. You can enable ContentControl.OnContentChanged to find out when the value of this property is updated.

+3
source

The cleanest way is to extract from this control and override the methods that report changes that interest you. For example, deduce from ContentControl and implement OnContentChanged . You may not like this approach.

If you want to detect changes in child elements or child elements of controls without receiving them, you may notice that such changes will affect the layout and therefore you can LayoutUpdated event. The problem with this approach is that you need to keep track of the children that were added earlier by checking Child or Children for changes. You should also be careful not to link ex-children, so as not to create a memory leak. But it can be done.

+1
source

Source: https://habr.com/ru/post/1340130/


All Articles