Silverlight's control library is constantly growing. Some controls are living in the assemblies and included in the Silverlight plugin install by default. These controls are usually referred to as the core controls. These core controls include all-time favorites such as TextBox, Button, CheckBox
, and so on. But the power of Silverlight doesn't come from its core components alone; you can always reference other assemblies that have other controls living in them and add them to your project. When you add such "outside" control to your page, you need to reference its namespace in your page.
Among Silverlight's core controls, there are controls we already know (and love) from other frameworks. Let's discuss the usage and properties of those controls. What is the first control that comes to your mind when you're thinking about text input? Most developers will answer—the TextBox
control.
The well-known TextBox
control has one purpose; to get text input from the user. Silverlight's TextBox
control can render itself in two modes—single line of text and multiline text area. The TextBox
control is very flexible, and its properties range from simple properties such as dimensions (height and width), font colors, and font size to more complex properties such as background or text wrapping. The following table lists the most frequently used properties of TextBox:
Name |
Description |
Possible Values |
---|---|---|
|
The text that will be displayed inside the textbox. It can be used in code behind to both get and set the text. |
Any text input. |
|
This dictates the height of the textbox. |
A number. |
|
This dictates the width of the textbox. |
A number. |
|
This dictates the vertical alignment of the text. |
|
|
This dictates the horizontal alignment of the text. |
|
|
This sets the background color of the textbox. It can be used as an element property as well, which gives the property some extra flexibility with brushes such as |
Color name or hexa-decimal value of a color (ARGB), for example, #FF0000FF. |
|
This sets the font family that will be used to render the text. |
A selection of fonts. |
|
This sets the size of the font. |
A number. |
|
This sets the color of the text. |
Color name or ARGB value. |
|
This sets the weight of the font. |
A list of selection ranging from |
The basic syntax for a TextBox
control is as follows:
<TextBox Text="Look ma! I'm a TextBox! "/>
Using the basic syntax, your TextBox
will render using the default black color and in the default font size and font family (Portable User Interface).
The TextBlock
control is like the TextBox
control's little brother. While TextBox
is used to get user input, TextBlock
is used to display text to the user. TextBlock
controls share the same properties as the TextBox
control, with the addition of two special properties, as shown on the following table:
Name |
Description |
Possible Values |
---|---|---|
|
This is used to trim a string up to a certain width. If set to |
None (default) or |
|
The |
Negative |
An example for using the TextBlock
control is as follows:
<TextBlock Text="Look ma! I can be trimmed!" Foreground="Red" TextTrimming="WordEllipsis"/>
The preceding TextBlock
will render the text in red and trim it if the text is too long to fit in its allocated space.
An example of using the Run
element within a TextBlock
control is as follows:
<TextBlock FontSize="25"> <Run FontFamily="Comic Sans MS" Foreground="Red" Text= "Hey there! I'm Red!"/> <Run FontFamily="Arial" Foreground="Blue" Text=" And I'm Blue!"/> </TextBlock>
The preceding code snippet will render as follows:
Two completely different styles share the same TextBlock
element.
The Button
control represents a button (big surprise here right?). In Silverlight, you have more than one Button
control. Other than regular Button
controls, you can use HyperlinkButton, ToggleButton, CheckBox
(that's a button, too!), and RadioButton.
We will start by introducing the Button
control and then move on to the other types of buttons.
Just like most of the controls in Silverlight, you can set all the 'usual' properties on a button: you can specify its width, its height, its vertical and horizontal alignment, and font settings (weight, color, family, and so on). What makes the button a button is the fact that it has three basic states—normal, hover, and pressed. We will discuss the states later in this book but as a short introduction, states represent a control's look at a specific point in time, and can be used to animate the control's look. In the button's case, when you hover over a button, you will see that the button changes its color to blue. That's the hover state in action. The Button
control has two important properties—Click
and ClickMode
. The Click
property's value represents the name of the code behind method that will fire when a user clicks on the button. Let's demonstrate this. In your Silverlight application, add a button using the following line of code:
<Button Content="Click me!" Height="30" Width="130" />
If you build and run your application now, and click on the button, you'll notice that nothing happens. This is because we haven't set the Click
property. Right after the value of width, start typing Click=
and once you see<New Event Handler>
, click the Enter
button. Your button code should look like the following code snippet:
<Button Content="Click me!" Height="30" Width="130" Click="Button_Click" />
Switch over to your code behind file (MainPage.xaml.cs
), and you'll notice there is a method called Button_Click
there. Visual Studio created it when you set the Click
property value. Add the following line of code to the Button_Click
event handler:
MessageBox.Show("I GOT CLICKED!");
Build and test your application. You'll get a nice message box each time you click the button.
The ClickMode
property sets when the Click
event should occur. This property has the following three possible values:
Hover:
The Click
event will occur every time you hover over the button.Press:
The Click
event will occur as soon as you click on the button.Release:
This is the default value. The Click
event will occur as soon as you release the mouse button.Try to set your button's ClickMode
property to all three values and watch how your button changes its behavior.
The HyperlinkButton
control is really a button that acts as a hyperlink. The HyperlinkButton
control's most important property is NavigateUri. NavigateUri
gets or sets the URL that the button should navigate to once clicked. No hyperlink will be complete without a property to specify its target. The HyperlinkButton
control has that property and it is named TargetName
. You can set the value for TargetName
the same as you would for a regular HTML<a>
tag target property such as _blank, _parent, and so on. For a list of all possible values, check out the MSDN documentation for HyperlinkButton.TargetName Property at http://msdn.microsoft.com/en-us/library/system.windows.controls.hyperlinkbutton.targetname%28VS.95%29.aspx.
The following line of code demonstrates the use of a HyperlinkButton
control:
<HyperlinkButton NavigateUri="http://www.packtpub.com" Content="My Publisher"/>
ToggleButton
is the base class for the CheckBox
and RadioButton
controls, but it can also be used as a standalone control as well. ToggleButton,
as the name suggests, is used in the scenarios where states need to be toggled around. ToggleButton
, being a button itself, has all the properties and events of a regular button, in addition to two very conceptual events—Checked
and Unchecked. Checked
occurs when the state of the ToggleButton
has changed to checked, while Unchecked
occurs when the state has changed to unchecked. Another property that is special to the ToggleButton
control is IsThreeState
. This Boolean property, when set to True
, adds a state to ToggleButton
named Indeterminate
. Let's examine how to use the ToggleButton
control with three states:
<StackPanel> <ToggleButton IsThreeState="True" Indeterminate="ToggleButton_Indeterminate" Unchecked="ToggleButton_Unchecked" Checked="ToggleButton_Checked" Content="Click me!"/> <TextBlock x:Name="tbState"/> </StackPanel>
The preceding code snippet adds ToggleButton
, which has three states. Each state is represented by an event handler. A TextBlock
is added to display the ToggleButton
control's current state.
Switch over to the MainPage.xaml.cs
file, and add the following code snippet below the constructor:
private void ToggleButton_Indeterminate(object sender, RoutedEventArgs e) { tbState.Text = "Indeterminate"; } private void ToggleButton_Unchecked(object sender, RoutedEventArgs e) { tbState.Text = "Unchecked"; } private void ToggleButton_Checked(object sender, RoutedEventArgs e) { tbState.Text = "Checked"; }
Each event handler will change the text property of tbState
to the state's name.
Build and run the application, and you'll notice that every click on the button changes the text according to the state the button is currently in.
Checking the ToggleButton state programmatically
You can always check what state ToggleButton
is in, from code behind using the IsChecked
property. If the ToggleButton
control is checked, the property will return true
, if unchecked false
, and if indeterminate the ToggleButton
control will return null.
The Silverlight CheckBox
control is similar to any other checkbox control you know from other platforms. As the CheckBox
control's base class is ToggleButton
, it uses the same events and properties as its parent class. To mark a CheckBox
control as checked, we set the IsChecked
property to true
and to react to checked and unchecked events, we set the Checked
and Unchecked
events handlers. A commonly used CheckBox
control's line of code will look as follows:
<CheckBox Content="Check me!" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
In your code behind file, you will have two event handlers as follows:
private void CheckBox_Checked(object sender, RoutedEventArgs e) { MessageBox.Show("I'm checked!"); } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { MessageBox.Show("I'm not checked!"); }
Build and run the application. The result of the preceding code snippet will render a checkbox with the content Check me!
, which will pop up a message box with some text once checked or unchecked.
Just like the CheckBox
control, the RadioButton
control also derives from ToggleButton
. The main difference between the RadioButton
control and the CheckBox
control is that only one radio button can be selected within a given group. To specify a group, the RadioButton
control uses the GroupName
property. Using the RadioButton
control is relatively simple. Its use can be outlined by the following code snippet:
<StackPanel Orientation="Horizontal"> <RadioButton GroupName="Group1" Content="Choose me!"/> <RadioButton GroupName="Group1" Content="No! Me!"/> <RadioButton GroupName="Group2" Content="I don't belong here..."/> </StackPanel>
The preceding code snippet adds three radio buttons, two from the same group and one from a different group. Build and run the application, and you'll notice that you can only select one radio button from the first pair, but the last radio button can be selected regardless of the others.
Take a journey down memory lane and try to remember the last time you've used the HTML button control. That button control knew how to work with just one specific type of content, text. To make a button use different content, such as an image, you have to use Cascading Style Sheets (CSS) and position a background image. But, what if you wanted that button to be a movie clip for example? You simply couldn't. Silverlight (and WPF) introduced a much more flexible model to work with controls' content. A Silverlight control can indicate whether or not it supports the inclusion of any type of control (text, image, or even a layout control) or just the inclusion of a predefined known item type.
Every content control is based upon the ContentControl
class, which in turn is based on the Control
class. As the base class of ContentControl
is Control
, any content control element features all of the properties, events, and methods that any normal control has.
The content of a content control can be any arbitrary object. If Silverlight cannot natively add the content to the visual tree, the ToString
method of the object will be called and its result will be displayed. This is the reason why a content control can have either simple text or a complex element as its content. To demonstrate how to set the content for a content control, let's add two buttons to our project:
<StackPanel> <Button Width="200" Height="100" Content="I'm a text based button!"/> <Button Width="200" Height="100"> <Button.Content> <StackPanel> <TextBlock Text="I'm a video playing button!"/> <MediaElement Source="/demo.wmv" Height="70" Width="100" Stretch="Uniform"/> </StackPanel> </Button.Content> </Button> </StackPanel>
The preceding code snippet renders two buttons—one with simple text content, and the other with a movie and text as its content. The resulting UI will look like the following screenshot:
As we can see from the preceding example, the content of a button, which is a content control, can be anything from a simple line of text to just about any UI control Silverlight has!
The magic that makes all of this happen is the ContentPresenter
element. The ContentPresenter
element holds the logic that the control needs in order to render any object passed on to it. ContentPresenter
uses TemplateBinding
to bind an element in XAML to a dependency property in the control's implementation. We will discuss TemplateBinding
and ContentPresenter
in much more detail later in this book.
3.148.107.254