views:

199

answers:

2

I am designing a user control in sliverlight that inherits from canvas. It is necessary for me to find out when a new UIELement is added to Children property of MyBase but there is no event like "ItemAdded". Since I want to animate the children of my canvas, I can not use LayoutUpdated event (It is hit a million times).

+1  A: 

When a child is added/removed the Canvas it will automatically be invalidated so that a Measure/Arrange cycle occurs. So you do not need to explicity track the adding/removing operations.

Instead you just need to override the MeasureOverride method and then inside the method you can look at the set of Children and notice what has changed. This is one area that Silverlight is much harder to use than WPF. You cannot provide your own collection for storing children like WPF and cannot hook event on the existing Children collection.

Phil Wright
A: 

Note that I have no Silverlight expierence, but in WPF you can use the Loaded and Initialized events on a control.

More info on a blog from Mike Hillberg from MSDN

Initialized Event

The Initialized event typically fires when the properties of an element have all been set. Specifically, FrameworkElement/FrameworkContentElement implement ISupportInitialize, and when the EndInit method of that interface is called, the IsInitialized property is set to true, and the Initialized event is fired.

Loaded Event

The Loaded event fires when an element is not only initialized, but it is about to be rendered. The motivation for the Loaded event is the typical scenario where you want to do some initialization in your application at load-time.

Arcturus