
The Role of the UserControl Type
In addition to the Application type, a Silverlight application will define a class extending UserControl. This
represents the UI and functionality of the Silverlight plug-in itself. Visual Studio names this class Page, and is the
same class which is set to the RootVisual property with the application Startup handler.
The initial XAML definition of the Page class is quite simple. Again, the x:Class attribute is used to connect this
markup to a related class definition. Note that a <Grid> is used as the default layout manager, however you can
change this to a different container if need be. Also notice the Height and Width properties are set in the opening
<UserControl> element.
<UserControl x:Class="SimpleSilverlightApplication.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
The initial code file is also straightforward. Notice the Page is-a UserControl. Similar to the Application derived
type, InitializeComponent() is within a compiler generated partial class file.
// C#
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
}
' VB
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
End Class
UserControl is defined within the System.Windows.Controls namespace. You will come to know the role of many
of these base classes during the remainder of this class. Here is a high-level overview of the role of each class in the
inheritance chain.
The UserControl parent class allows derived types to host ‘content’. The Silverlight content model demands that a
UserControl specifies a single piece of content. Most often, the ‘single piece of content’ will be a layout manager
containing all the UI elements, graphics, etc.
The Content property of UserControl can be used to define the content to display in code. In XAML, the first sub
element of the <UserControl> scope will be used to implicitly set the Content property.
In addition to the Content property of UserControl, be aware that a majority of Silverlight controls extend the
ContentControl parent class. This class also defines a Content property for a similar purpose. For example, the
Button class extends ContentControl, and therefore can participate in the Silverlight content model.
By way of an example, a Button could maintain an inner StackPanel as ‘content’. The StackPanel contains an
Ellipse and Label. You will examine the details of the content model in a later chapter. For now, here is a simple
example in XAML.
<UserControl x:Class="SimpleSilverlightApplication.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<!-- The Content of this UserControl is the following Button -->
<Button Height = "150" Width = "120">
<!-- This button has a StackPanel as content. -->
<StackPanel>
<Ellipse Fill = "Orange" Height = "75" Width = "75"/>
<TextBlock Text = "OK!" FontSize = "20" HorizontalAlignment = "Center" />
</StackPanel>
</Button>
</UserControl>
The Control parent class defines a number of members that give derived types their core look and feel. Properties
exist to establish the control’s opacity, tab order logic, background color, font settings, and so forth. The Control
type also provides key infrastructure to apply templates and styles to a UI widget. You will learn about templates
and styles later in the course.
FrameworkElement is another key parent class to many UI widgets in that it provides members to control size,
tooltips, mouse cursor, and other settings. This class also provides support for WPF animation and data binding
services.
UIElement provides the ability to process mouse and keyboard input. This class also contains properties to control
visibility, and geometric transformations.
DependencyObject is the parent that provides derived types the ability to work with the ‘dependency property’
model. As you will see later, a dependency property makes it possible for a property to receive input from multiple
locations. This class provides access to the app’s lower-level event queue via the Dispatcher
The Role of the UserControl Type
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services