Wednesday, February 6, 2008

 

WPF Frame, Content and ContentRendered

This is something that has bit me in the past, so I though I would mention it here. The .NET Framework 3.0 has the WPF extensions, including the System.Windows.Controls.Frame class. And, the Frame class as a Content property. But, if you have an instance of some WPF object (perhaps a System.Windows.Controls.Canvas) and set it to the Content property of the frame, the frame's Content may return null.


System.Windows.Controls.Frame myFrame =
new System.Windows.Controls.Frame();

System.Windows.Controls.Canvas myCanvas =
new System.Windows.Controls.Canvas();

myFrame.Content = myCanvas;
if (myFrame.Content == null)
{
Debug.Assert(false, "Why is the Content property still null?");
}
 

So, what's going on? How can myFrame.Content still be null after I just set it?

The answer is straight forward, if you know what's going on. The Frame class renders its contents asynchronously. And, when the Frame.Content property is set, the property returns null until the frame has fully rendered its contents. Once the contents have been rendered, however, it will return the object you expect. You can catch the frame's ContentRendered event if you need to know when rendering has completed.

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]