Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.
Recent comments

In order to get the value of your QueryString parameters from the URL all you need is this line of code:

int id = HtmlPage.Document.QueryString["ID"];

And you need to add Windows.Browser namespace for HtmlPage class:

using System.Windows.Browser;

However, there are always two situations which could break your application:

  1. if there are no “ID” parameters in the url
  2. if there is an “ID” parameter in the url, but it has a string value instead of an integer number.

To avoid breaking the code and causing runtime error we need to add a few conditions and also parse the value to integer instead of assigning it right away. We can use Int32.TryParse() method to test the value which we are about to assign to an integer variable before we actually do so, this way we will only assign it if it really is an integer:

int id = -1;
if (HtmlPage.Document.QueryString.ContainsKey("ID"))
    string queryStringValue = HtmlPage.Document.QueryString["ID"];
    if (Int32.TryParse(queryStringValue, out id))
        // the result was successful and
        // the correct ID will be inserted to id
        // the result was not successful                   

Using above method, you ensure the id gets assigned a value only if the ID parameter in the url has a valid integer value, or it will remain as -1 if the value was not an integer.

Posted by Damon Serji on 30. September 2009 22:30 under: Basic, Intermediate
 with 0 Comments

Gavin Wignall at Silverlight Buzz is doing a series of posts on his blog, aiming to cover the most useful information and techniques on Expression Blend for both beginners and pros. For more information, and to see the list of topics, which will be posted through the whole October, visit Learn Expression Blend in a Month! at

Posted by Damon Serji on 30. September 2009 21:26 under: Silverlight News
 with 1 Comments

In this example I will demonstrate how to use ImageBrush to fill area of shapes like rectangle or ellipse with an image.

In order to fill the area of a control like Rectangle control, we need to create an instance of type BitmapImage, and this is not possible if we do not have a Uri which points to a real image file.

So first, I create an Uri pointing to an image which I had put in the “images” folder in my Silverlight application project.

Hint: try to use Uri.TryCreate to create your Uri as by this way you stop your application from breaking if the path you gave your Uri was not valid or did not exist.

Second, create a new instance of BitmapImage and assign your Uri to its value.

Uri imageUri;
BitmapImage image = null;

if (Uri.TryCreate("images/SilverlightTips.jpg", UriKind.RelativeOrAbsolute, out imageUri))
    image = new BitmapImage(imageUri);

Now you can create a new instance of ImageBrush and assign your created BitmapImage to its ImageSource value:

ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = image;

Finally, using .Fill method on your Rectangle control fill the entire rectangle (or any other shapes your object is) with your ImageBrush. And here is how your final code should look like:

Uri imageUri;
BitmapImage image = null;

if (Uri.TryCreate("images/SilverlightTips.jpg", UriKind.RelativeOrAbsolute, out imageUri))
    image = new BitmapImage(imageUri);
if (image != null)
    ImageBrush imageBrush = new ImageBrush();
    imageBrush.ImageSource = image;
    Logo.Fill = imageBrush;
    LogoEllipse.Fill = imageBrush;
    LogoRectangle.Fill = imageBrush;

Download this project from CodePlex from here. Here it is working:

Posted by Damon Serji on 28. September 2009 13:42 under: Intermediate
 with 2 Comments
Get Microsoft Silverlight

Silverlight Child Window is a great new feature which enables you to use advantage of a fully enhanced popup window. Child Window can be customized just like a normal xaml file to display or request information from the user. In this post I will explain briefly how receive and save information entered by user from a Child Window (ChildWindowForm.xaml in our example) and then display them in the parent window (MainPage.xam).

I am going to create an example in which user enters his/her name and surname in a Child Window and once submitted different details, along with the entered name and surname, are displayed in the main page.

I have uploaded the working example of this post to CodePlex and you can download it from here to test it for yourself.

1. add a new Child Window
in Visual Studio = right click on your Silverlight project, then, Add > New Item > Silverlight Child Window. The Child Window will have an OK and a Cancel button by default when it is created. Add two TextBox controls to the page for users to enter their name and surname.

2. Create an EventHandler in Child Window
code to create a new EventHandler:

public event EventHandler SubmitClicked;

3. Create public properties to save the values
it is possible to access the value of the TextBox controls we added in the Child Window directly from the MainPage.xaml.cs, however, this is a terrible practice as it will reduces your code stability in the future. Therefore create two public properties of type string to save the value of both TextBox controls. You will notice in my project I save the value of the TextBox in a private string variable and then save that variable to my public – this is just a better practice for coding so don’t get confused.

public string NameText
    get { return _nameText; }
        _nameText = value;
public string SurnameText
    get { return _surnameText; }
        _surnameText = value;

4. Fire the event once submit button is clicked in Child Window
there should already be a method for when the OK (Submit) button is pressed, it is called OKButton_Click. Inside that we need to fire the event which we created earlier, so that our event gets fired when the code reaches this method i.e. when we press OK in Child Window.

private void OKButton_Click(object sender, RoutedEventArgs e)
    if (SubmitClicked != null)
        _nameText = Name.Text;
        _surnameText = Surname.Text;
        SubmitClicked(this, new EventArgs());
    this.DialogResult = true;

5. In MainPage.xaml create a new instance of the Child Window

in order to display your Child Window first create a private variable your Child Window type - what ever you called your Child Window file will be the type of your variable i.e. ChildWindowForm.xaml:

private ChildWindowForm childWindowForm;

now create a new instance of it:

childWindowForm = new ChildWindowForm();

6. How to show Child Window
you need to display the actual Child Window which you created and have the instance in your MainPage. To do that, use the instance you just created and call the build-in method for any Child Window called “Show”:


7. In MainPage.xaml hook up the submit event in Child Window
now we have an event which will get fired up from the Child Window when we click the submit button (OK button). All we have to do is to wire that event up so that we can do something when it is fired:

childWindowForm.SubmitClicked += new

8. Access the properties of the Child Window

private void childWindowForm_SubmitClicked(object sender, EventArgs e)
    Name.Text = childWindowForm.NameText;
    Surname.Text = childWindowForm.SurnameText;

I have skipped explaining some features that you will see in the example I created for this project (download from here) to concentrate more on the subject of this post. However if you have any questions about this post or the example please do feel free to post your comments and I will try to help as much as I can.

Posted by Damon Serji on 24. September 2009 21:27 under: Intermediate, Intermediate
 with 8 Comments

You could use a variable globally in your .NET website if you declare it in your Web.config file.

in your Web.config file find <appSettings> tag and insert your variables before it's closing tag:

  <add key="MyVariable1" value="False" />
  <add key="MyName" value="Damon Serji" />

Now you can easily access this variable and its value from your C# code:

string myVariable = System.Configuration.ConfigurationSettings.AppSettings["MyName"];

To break the above code down and to make sense of it: System really looks in your Web.Config file and the rest of the line becomes more obvious when you look at your Web.Config structure:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration xmlns="">
       <add key="MyVariable1" value="False" />
       <add key="MyName" value="Damon Serji" />

So your Web.Config is nothing but a simple XML. System.Configuration tells the compiler to look in to <configuration> tag and System.Configuration.ConfigurationSettings.AppSettings tells it specifically to look into the <appSettings> tag. The result is a simple array of other tags which were added by you manually, so to find a specific tag you need to give it the index of the array i.e. an integer, or the "key" for that array, which is another way to find the tag you are looking for.

Posted by Damon Serji on 22. September 2009 18:06 under:
 with 0 Comments

meta tags contain useful information that could be used by user’s browser, search engine optimization, Java Script and etc. Some of the most common ones are "keyword" and "Description" meta tags which are used for better optimization.

Changing/Adding these meta tags is particularly useful if you display different data according to user’s setting or your application setting, e.g. if you have more than one language and want to have a different keyword meta tag for each language.

Here is how to add meta tags to your site programmatically:

HtmlMeta htmlMeta = new HtmlMeta();
htmlMeta.Name = "keywords";
htmlMeta.Content = "Tips, Silverlight Tips,";

Above code will generate the following code in your .aspx file:

<meta name="keywords" content="Tips, Silverlight Tips," />

Posted by Damon Serji on 22. September 2009 17:39 under: Intermediate
 with 0 Comments

In order to call a function that you created in your .HTML or .ASPX file (which hosts your Silverlight application), you need to Invoke that function by using Invoke method in System.Windows.Browser.HtmlPage namespace:


If you want to pass some parameter to that function, simply add the parameters to the method using comma.

HtmlPage.Window.Invoke("yourFunction", param1, param2);

Posted by Damon Serji on 22. September 2009 17:29 under: Intermediate
 with 1 Comments

This post explains how to add localization to a Silverlight application.

1. Adding a resource file (.resx) to your Silverlight project
first add a new folder to your Silverlight application and call it anything you like, i.e. LocResources, and then in your Visual Studio add a new Resource File in that folder: Add>New Item>(General category)Resources File. Then, add a new resource to your file, i.e. SiteTitle, and save it. While the resource file is opened, change the Access Modifier to Public (using the combo box).

Now copy and paste the .resx file in your LocResources folder and rename the new file to

Very important: Open your resourcefile.Designer.cs file and change the Constructor to public, i.e. open SiteContent.Designer.cs and change internal SiteContent() to public SiteContent(). This will allow your MainPage to have access to this file.

A .resx file is really nothing but a XML file that contains your content in a format which can be used to localize your application, try opening the .resx file with Notepad to see it for yourself.

Please note, you can call your .resx file anything you like, but unless it is the default language of your site it has to end with the local indicator characters, i.e. if it is French your file has to be called

2. Add a Resources namespace and a static resource


Add the above namespace accordingly, where name is your Silverlight application name. Then add a static resource:

   <MyResources:SiteContent x:Name="SiteContent"/>

3. Replace the hardcoded text with Static Resources
update Silverlight controls to have the following format where they display text:

Text="{Binding SiteTitle, Source={StaticResource SiteContent}}"

SiteTitle is the name of your resource data, you added this in step 1 in your resource file (.resx). SiteContent is the x:Name we gave to our reference of resource file which we created in step 2.

4. Update the .csproj file of your Silverlight project
open the .csproj file of your Silverlight project in Notepad, locate the SupportedCultures element, at the moment it should not have any value, you will need to change this to a different value if you were going to localize it for a different language than your default language, i.e. you would needed to change this to fr-FR if you were going to localize this for French language, so change this to <SupportedCultures>fr-FR</SupportedCultures> then save and close the file.

5. add InitParameters to the Silverlight control
open your TestPage.aspx (the default page that hosts your Silverlight Control in your web application project), and add InitParameters="UICulture=fr-FR" in order to localize for French. If you are using Object tag add <param name="uiculture" value="fr-FR"/>, and if you are using <asp:Silverlight ... tag add this: InitParameters="UICulture=fr-FR" to your tag.

6. Update App.xaml.cs
copy the following code to the Application_Startup method:

if (e.InitParams.Count > 0)
    string cultureName = e.InitParams["UICulture"].ToString();
    if (!String.IsNullOrEmpty(cultureName))
        Thread.CurrentThread.CurrentUICulture =
        new CultureInfo(cultureName);

You may need to addthe following namespaces to the top of the page:

using System.Threading;
using System.Globalization;

Run and enjoy!

Posted by Damon Serji on 15. September 2009 22:11 under: Intermediate
 with 2 Comments

The trick is to capture the selected index of the item which has been clicked, and then assign it back to the ListBox control by using .SelectedIndex method.

The only thing I needed to do extra was to assign the .SelectedIndex to 0 before I assign it back to the actual selected index.

Also, you will see in this example that I am capturing the selected index number on .MouseLeftButtonUp of TextBlocks which are my LibBox’s items, you could off course capture this information on .Click or . MouseLeftButtonUp of any other control in your ListBox items.
You can download the working project from here.

To test the project click on an item in the ListBox, then click on Loose Focus button, and then click on Focus button to bring the focus back to the ListBox.

Posted by Damon Serji on 15. September 2009 10:33 under: Intermediate
 with 0 Comments

To set the ZIndex of a Silverlight control programmatically use:

Canvas.SetZIndex(_control, zIndex);

Where _control is your Silverlight control and _zIndex is an integer number which you would like the new ZIndex of your control to be assigned to.

Posted by Damon Serji on 12. September 2009 16:55 under:
 with 2 Comments