Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What event is fired when a UserControl is displayed?

Tags:

wpf

I'm trying to add a fade effect (animation) for WPF UserControls (although rather for FrameworkElement, to make it more general).

If I let the Loaded event trigger the initial fade-in, the fade animation will sometimes have started before the UserControl has been displayed. The result is a mess. That happens for example if the UserControl does something lengthy (a few tenths of a second), like execute a query, in the Loaded event handler.

So, I would need to handle some event that FrameworkElement/UserControl gets when the content has been rendered, and start the fade-in then. The System.Windows.Window has a ContentRendered event, but UserControl has not. So, what event is fired when a FrameworkElement (or UserControl) has been rendered?

like image 500
John Reynolds Avatar asked Jan 16 '11 21:01

John Reynolds


1 Answers

Try to check size on SizeChanged or LayoutUpdated. Do job when actual width or height not equals to 0.

view.LayoutUpdated+=(o,e)=> {   if (!loaded && (view.ActualHeight > 0 || view.ActualWidth > 0))   {      // You can also unsubscribe event here.      loaded =true;   } } 
like image 103
Ievgen Avatar answered Sep 19 '22 22:09

Ievgen