Silverlight Tutorials Chapter 4: Introduction to Controls

In my earlier three chapters of Silverlight Tutorial, I described about “Introduction to Silverlight”, “Introduction to Silverlight Application Development” and “Introduction to Panels”. I got lots of responses & wishes from my readers for the next chapter. I know, there was a big gap between these chapters and I am really apologizing to you. Thank you for your passion. Hope, this time also I will get more responses from you. Read the complete tutorial and provide your valuable feedbacks. You suggestions/comments are highly appreciated.


In this Chapter, I will describe you about some of the most common controls used in Silverlight. After the end of this section, you will be able to understand about those basic controls and their uses. Here I will guide you to understand some of the properties of each control and how to set them from both XAML & Code behind C# file.



Silverlight 4 provides a large collection of UI controls for Silverlight application development. These controls are require to do the UI design of the application for giving a better representation to the user. You can customize the look & feel by creating your own Style or extending the control to give more power to the UI. I will describe the control styling in later chapters.


Here is some of the Silverlight 4 controls that I want to describe for you:

  1. TextBlock
  2. TextBox
  3. PasswordBox
  4. RichTextBox
  5. ComboBox
  6. ListBox
  7. CheckBox
  8. RadioButton
  9. Button


TextBlock is the primary element for displaying text inside your Silverlight Application. You will find it useful whenever you want to display some strings. TextBlock is a FrameworkElement available in the System.Windows.Controls namespace. There is a property named “Text” where you have to enter the text string.

<TextBlock Text="Hello Silverlight" />

This is not necessary for you to use the Text property to set some text inside your TextBlock control. TextBlock control can also be used as a container to set a para of strings. Hard enter to place a line break can be set using the <LineBreak/> control.



         This is the first line of Text.


         This is the second line of Text.





     This is the first line of Text.


     This is the second line of Text.


Also, you can represent a series of strings contained inside different Run elements instead of presenting as a single string. In that scenario, each Run element can have different font attributes set towards it. The LineBreak element will represent an explicit new line break



     <Run Foreground="Maroon" FontFamily="Arial" FontSize="30"

              FontWeight="Bold">Arial Bold 30</Run>


     <Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18"

              FontStyle="Italic">Times New Roman Italic 18</Run>


     <Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14"

              FontWeight="Bold">Verdana Bold 14</Run>



You will see various Properties and Events available for TextBlock control to do text formatting and other operation.




TextBox is another important element in Silverlight. It is also available inside the System.Windows.Controls. Unlike TextBlock, the TextBox inherits from the base class Control. Whenever you want to enter some text inputs from your user using your Silverlight application, you need the help of TextBox. It is one of the most important input control for Silverlight for data entry.

<TextBox Height="25" Width="200" Text="This is a Single Line TextBox"/>


<TextBox Height="100" Width="200"




                Text="This is a Multi-Line TextBox. This is a Multi-Line TextBox."/>

clip_image008 clip_image010

Here are some of the DependencyProperty registered with the TextBox control:


public static readonly DependencyProperty AcceptsReturnProperty;

Set “AcceptsReturn” to True if you want to insert a Hard line break explicitly in your TextBox by your user. From code behind if you want to set the value to AcceptsReturnProperty of the TextBox, you have to call the SetValue() and then pass the DependencyProperty with the appropriate value:

myTextBox.SetValue(TextBox.AcceptsReturnProperty, true);

myTextBox.SetValue(TextBox.AcceptsReturnProperty, false);

From XAML, you can set it as:

<TextBox AcceptsReturn="True"/>

public static readonly DependencyProperty IsReadOnlyProperty;

Set “IsReadOnly” to True if you want your TextBox as only readable by your user. This is very useful when you are putting a Edit Button near your TextBox to give the user chose whether he wants to update some texts.


To set it from the code behind use the same format of the SetValue() method. Here’s the code:

myTextBox.SetValue(TextBox.IsReadOnlyProperty, true);

public static readonly DependencyProperty MaxLengthProperty;

Set “MaxLength” to a positive numeric value , your user can’t add more than the specified value. Suppose you are setting it to 50, hence after inserting 50 characters the TextBox caret will not go forward.


You can set the Max Length property from code behind C# like this:

myTextBox.SetValue(TextBox.MaxLengthProperty, 150);

public static readonly DependencyProperty SelectionBackgroundProperty;

“SelectionBackground” color once set, your selected text will have the specified background. You can set both Solid Brush or Gradient Brush as your selection background. If you want to set it from code behind, use the following convention:

myTextBox.SetValue(TextBox.SelectionBackgroundProperty, new SolidColorBrush(Colors.Black));

public static readonly DependencyProperty SelectionForegroundProperty;

Like “SelectionBackground” the “SelectionForeground” will set the specified color as the foreground color of the selected text. The property SelectionForeground property can also be set in the same way. In this case, it will change the color of the font for the selected text. Have a look:

myTextBox.SetValue(TextBox.SelectionForegroundProperty, new SolidColorBrush(Colors.Black));

public static readonly DependencyProperty TextAlignmentProperty;

“TextAlignment” is responsible to set the text alignment to left, right, center or justify.


public static readonly DependencyProperty TextProperty;

“Text” property contains the Text entered into the TextBox. You can set it in the same way from XAML & Code as I mentioned above.


public static readonly DependencyProperty TextWrappingProperty;

Set “TextWrapping” to Wrap so that, if your entered text becomes longer than the specified width of the TextBox, it will populate the remaining text in a separate line (as described in the MultiLine TextBox figure).



PasswordBox is available inside the System.Windows.Controls and inherits from Control. Whenever you need to add a Password field in your Silverlight application form to give the user access to login with credential, you have to use this password box. You will find it useful when you need to do login.

<PasswordBox Width="200" Height="25" Password="kunal2383" />

<PasswordBox Width="200" Height="25" Password="kunal2383" PasswordChar="X" />


Here are some useful DependencyProperty of the PasswordBox:


public static readonly DependencyProperty MaxLengthProperty;

Set “MaxLength” to a positive numeric value , your user can’t add more than the specified value. Suppose you are setting it to 50, hence after inserting 50 characters the TextBox caret will not go forward.


public static readonly DependencyProperty PasswordCharProperty;

Sets the password character of the control. The default value for password character of the field box is the asterisk (*). But you can change it by setting the value to this dependency property. If you set ‘X ‘ as password character, whatever you enter in the textbox of the field will display as ‘X’.


public static readonly DependencyProperty PasswordProperty;

Gets or Sets the password property of the PasswordBox. If you set the text it will be visible in the UI in asterisk format.


public static readonly DependencyProperty SelectionBackgroundProperty;

“SelectionBackground” color once set, your selected text will have the specified background.


public static readonly DependencyProperty SelectionForegroundProperty;

Like “SelectionBackground” the “SelectionForeground” will set the specified color as the foreground color of the selected text.



Like TextBox RichTextBox also inherits from Control and available in the namespace System.Windows.Controls. It is useful when you want to give a input box to your user where they can able to format the entered text like word processor. You can display text in various text format like Bold, Italic, Underline, Left/Right/Center align, various font size/color etc.


Have a look into the following XAML code where the RichTextBox has several <Paragraph> tag to split the text in various parts. You can use <Bold>, <Italic> tags to format the entered text. You can also align the text or change the font-size or font-color by setting the proper attributes to the <Paragraph> tag.

<RichTextBox Height="153" Width="336" AcceptsReturn="True">


          This is a RichTextBox control having multiple paragraphs inside it.

          You will find it more useful while creating an Editor control in Silvelight application.

          <LineBreak />



          <Bold>This Text is in Bold</Bold>

          <LineBreak />

          <Italic>This Text is in Italic</Italic>


     <Paragraph TextAlignment="Right">

          This Text is Right aligned.

          <LineBreak />


     <Paragraph TextAlignment="Center">

          <Hyperlink>This is an Hyperlink Button and center aligned</Hyperlink>



Here is the output of the above XAML code:


You can see here that, not only formatted text you can also enter hyperlinks to the RichTextBox control. If you set the AcceptsReturn=”True” your user can explicitly enter hard line break by pressing the “Enter” key.



ComboBox is an items control which works like a ListBox but only one item is visible at a time. It inherits from Selector control and available in the System.Windows.Controls namespace. It contains ComboBoxItem which inherits from ListBoxItem. ComboBoxItem is also present in the same namespace as ComboBox is i.e. System.Windows.Controls namespace.


Let us create a simple ComboBox having some contents inside it. Set the Width of the ComboBox to a specific value, let’s say 200 in our case. Now add ComboBoxItem as the Items of the Combo. This step is not always require (in case of XAML atleast). Set some string as the “Content” of the ComboBoxItem. Here you may ask one question, is it only supports text? The answer is “No”. You can also set any FrameworkElement inside the Content of the ComboBoxItem, such as TextBlock, TextBox, Button, Rectangle etc.


Have a look into the XAML code here:

<ComboBox Width="200">


          <ComboBoxItem Content="Item 01"/>

          <ComboBoxItem Content="Item 02"/>

          <ComboBoxItem Content="Item 03"/>

          <ComboBoxItem Content="Item 04"/>

          <ComboBoxItem Content="Item 05"/>

          <ComboBoxItem Content="Item 06"/>

          <ComboBoxItem Content="Item 07"/>

          <ComboBoxItem Content="Item 08"/>

          <ComboBoxItem Content="Item 09"/>

          <ComboBoxItem Content="Item 10"/>



The output of the above XAML will initially look like the below figure:


Now once you click on the small arrow at the right side of the ComboBox, you will see a menu comes out from it containing the content you added there:


Here are some properties those you will find useful while working with the ComboBox:



Once set to True the drop down will show with the content. By using this property you can programmatically control the visibility of the dropped menu. By default it is set to False and when you click on the small arrow it sets it to True and thus it opens up in the screen.



From the name you can easily understand the functionality of this property. By default it is set as False. Hence, in the normal scenario you can’t type anything inside the ComboBox. If you want to edit the content of the ComboBox or want to type inside it, just set the value to True.



You can control the size of the drop down menu. If you set it to some numeric value, the height will be set as per the entered value.



ListBox inherited from Selector present inside the System.Windows.Controls namespace. It contains ListBoxItem as Items. It represents it’s children as list. The followings are the DependencyProperties of ListBox:

public static readonly DependencyProperty IsSelectionActiveProperty;

public static readonly DependencyProperty ItemContainerStyleProperty;

public static readonly DependencyProperty SelectionModeProperty;

Some important properties are described below:



Gets or Sets the Selection Behaviour of ListBox control. There are three different modes of the Selection of the ListBoxItem: Single, where user can select only one ListBoxItem at a time; Multiple, where user can select multiple items without pressing a Modifier Key; Extended, where user can select multiple items by pressing a Modifier Key. The default mode is “Single”.

<ListBox Width="200" SelectionMode="Multiple" Height="200">


           <ListBoxItem Content="Item 01"/>

           <ListBoxItem Content="Item 02"/>

           <ListBoxItem Content="Item 03"/>

           <ListBoxItem Content="Item 04"/>

           <ListBoxItem Content="Item 05"/>

           <ListBoxItem Content="Item 06"/>

           <ListBoxItem Content="Item 07"/>

           <ListBoxItem Content="Item 08"/>

           <ListBoxItem Content="Item 09"/>

           <ListBoxItem Content="Item 10"/>




The above example shows you how the ListBox looks like in different SelectionMode. The first example demonstrates “Multiple Selection Mode”. This type of listbox you need to give your user a choice to select multiple items. In the second example shows a simple “Single Selection Mode”, where the user can only select a single choice.



Checkbox inherits from ToggleButton and resides inside System.Windows.Controls namespace. Using this you can give your user a choice to select multiple items. There are three different states for CheckBox. If checked will return true, unchecked will return false and the third state returns null value. Generally the third state is not that much useful in normal scenarios. Use “Content” to set value (e.g. string, rectangle etc) to the checkbox.

Have a look into the following example:

<CheckBox IsChecked="True" Content="Set as Checked (True)" />

<CheckBox IsChecked="False" Content="Set as UnChecked (False)" />

<CheckBox IsThreeState="True" IsChecked="{x:Null}" Content="Set as Third State (Null)" />

<CheckBox IsEnabled="False" IsChecked="True" Content="Set as Checked, but disabled" />




                 <Ellipse HorizontalAlignment="Stretch" Fill="Red" />

                 <TextBlock Text="Multi Element as Checkbox Content" />





The first code demonstrates the Checked item, the second code demonstrates unchecked item and third code demonstrates the third state. Third states becomes null. In the fourth code you will see the disabled state of the checkbox. What about the fifth code? Yup, in the fifth code we have added a Ellipse and a TextBlock wrapped with a Grid panel. It is not always necessary to set string as content. As per your requirement you can set anything. But be sure that CheckBox.Content only supports a single element and if you want to set multiple elements wrap with a panel as I demonstrated above.



Like CheckBox the element name RadioButton placed inside System.Windows.Controls assembly namespace also inherits from ToggleButton and contains the same properties as described above in CheckBox examples.

<RadioButton IsChecked="True" Content="Set as Checked (True)" />

<RadioButton IsChecked="False" Content="Set as UnChecked (False)" />




                <Ellipse HorizontalAlignment="Stretch" Fill="Red" />

                <TextBlock Text="Multi Element as RadioButton Content" />




<RadioButton GroupName="MyGroup" Content="Set under MyGroup" IsChecked="True" />

<RadioButton GroupName="MyGroup" Content="Set under MyGroup" />


Normally if you are setting multiple radio buttons inside a page, you can select only one of them. If you need your user to select radio button from each different group of your option you can set a group name to the radio button. By grouping the options using a group name will allow your user to select a single radio option from each group.



Button control in Silverlight inherits from ButtonBase class and can be found under the System.Windows.Controls namespace. Button controls are mainly require to Submit forms or for navigation panels. You can style it as per your need.


You can set some text as it’s content or can place any framework element inside it. If you want to add more than one element inside it, you have to use any panel wrapped to your elements.


See the below code on how we can use Button controls:

<StackPanel x:Name="LayoutRoot" Margin="20" Background="White">

     <Button Content="I am a button control" Height="25" Width="200" Margin="5" />

     <Button Height="25" Width="200" Margin="5">


               <CheckBox Content="Checkbox inside Button" />



     <Button Width="200">


               <StackPanel Orientation="Horizontal">

                     <Rectangle Width="30" Height="25" Fill="Green" Margin="0,0,5,0" />

                     <TextBlock Text="Rectangle inside Button" />





The first example is a simple button control whereas the second contains checkbox as it’s content. The third example contains a Rectangle and a Text wrapped by Grid panel. This shows how we can use panel to load multiple elements inside a button.


Button has Click event where you can write your logic to do whatever you want once you click on the button e.g. navigation to a different view, submitting the form etc.

End Note

Don’t forget to share your feedbacks/suggestions. They are always welcome for the improvements of articles.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly.

Don't forget to share your feedback/queries in the comment section below.


  1. Hello Kunal. Good job man. Keep it up.

  2. Thanks Kunal for sharing with us.

  3. hi thanks 4 sharing the 4th chaptre on silvelight controls.

  4. Hi all,

    Thanks for your feedback.


  5. Good work dude.It would also help to showcase the same in code behind(RichTextBox in my mind)

  6. Hi Balaji,

    Thanks for the suggestion. I will update it soon with the code behind code.


  7. I love it... Keep it up

  8. Kunal,
    I really appreciate your tutorials about Silverlight. I am waiting for the new one ;)

    Thanks a lot,

  9. Thanks buddy. Also read my articles in CodeProject as well as in my Blog and vote for them.

  10. Hi Kunal,
    Thanks a lot for the tutorials. They are really lucid and very explanatory. I had a question for you though, is it possible to access silverlight controls from aspx code behind and also the vice versa without the initparam ? Thanks

  11. Ummm... You can't directly access as you normally do. But, you can access through the help of JavaScript.

  12. Hi Kunal,

    How can we read the content of button which is placed inside textblock?

    for example..i have a button and inside thati have placed a stackpanel and two textblocks how can i read the textblock contents?

    please suggest me.

  13. Your query is not clear. First you told, read content of button placed inside textblock. Then you told textblock inside button. Which one is correct?

    Hoping that, you have a textblock inside your button. Create a DependencyProperty and bind with the TextBlock. You will be able to read the property easily then.

    Another solution is: give a name to the textblock. Now from the button control find the textblock by providing the textblock name. Now you will be able to read the text.

    Let me know, if you want anything else.


  14. Hi Kunal,
    I need a brief tutorial about datagrid controls. Also expecting your future posts in the silverlight tutorial section very keenly :)

    Thanks and Regards,

  15. Hi Prathi,

    Glad to hear that you liked the tutorial. Appreciate your suggestion on the next chapter. I will try to write one chapter on the same.


  16. good job. keep it up. looking for next post.

  17. very Nice..

  18. Very good articles !! Looking for your next post

  19. Kunal u Really Great....

  20. I am not great. It's my passion to help the community.

  21. nice aritcles kunal ...keep sharing your ideas.thank you.

  22. Thanks a lot for all of your 4 chapters. It's very useful for me. Please keep it up..


  23. Hi, Very Usefull Information given,But I want to display my buttons as given in your home page
    (like Home, Silverlight-Zone, Silverlight-tutorial...),problem i am facing is ,wen i drag a button control its doesn't look similar..
    Can u pls help me out in this..


  24. Hi Nandish,
    You will not get similar look and feel using the default button template. You have to edit the button style to give similar look. Try out searching some tutorials on template/style editing using Expression Blend.

  25. Great Spirit brother...Keep it up and help us

  26. Really very very informative tutorials.. :)

  27. Dear Kunal,
    Thanks a lot for your great post.
    Please let me write if u have any post on silver light menu(Horizontal/Vertical)

    [email protected]

  28. It's a good introduction to control for beginners.

  29. it is very useful for the begineers..thankx alot!!!

  30. hai kunal

    your articles are very nice.

    i am new for sliverlight application.

    you give brief explain about all controls

  31. Very Nice tutorial ________i fall in love of SilverLight

  32. Very Good Article,Thank You

  33. Nicely explained. I want to have a control acting as a combo box but having check boxes in it. Means, a multi select combo box. Can you provide any help on this?

    1. You can modify the DataTemplate of your ComboBox to implement the same functionality. Let me know, if you need any further help.

  34. nice article. waiting for chapter 5

  35. i wanna add three hyperlink buttons in one column
    can it be????

  36. Thanks for taking so much time and effort to share your knowledge with all of us. I am a rank amateur and need some help on how to code a button on one page to hide or close the that page and open or show another page in VS 2010 and Silverlight. Any help would be greatly appreciated ! Thanks again.

  37. Nice work Kunal.Hope to learn Silverlight in an interesting way. And please provide us scenario based example also.

    keep up the good work. Nice job.


© 2008-2018 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top