I create a lot of demo applications, and with Silverlight 2 that means creating a lot of user controls. Sometimes I build entire applications and other times I build simple and focused user controls. Sometimes I have a dozen user controls that I use to demonstrate different topics at events. I created a simple custom user control that loads another user control on demand. This has been very helpful for presentations and for using samples for my Silverlight 2 book.
The custom control I created is called MenuView. It shows a ListBox on the left that displays the information about each user control that I want to load. When the user selects the ListBox item, the user control is loaded dynamically on the right.
Its pretty straightforward in appearance and can certainly be spruced up. The purpose is to allow me an easy way to dynamically load control I want to demonstrate. The basic structure of the MenuView control is a Grid (shown below). The left hand side holds the ListBox and the right hand side contains a StackPanel, which is where the user controls will be loaded dynamically.
The MenuView loads controls when the ListBox selection changes. It creates an instance of the user control using its type, and its the control to the StackPanel (shown below).
All of this in encapsulate din a Silverlight class library. This allows me to refer to this control in several applications simply by referencing the DLL. So in a Silverlight 2 application where I have several unrelated user controls that I want to use in a presentation, I create a list of MenuItem classes and pass them to the MenuView control. The MenuItem class is a simple class with Title, Description and ControlType properties. Title and Description are displayed in the ListBox menu. The ControlType is used to dynamically load the user control when the selection is made.
If interested, the source code for this control can be downloaded here.