Click here to Skip to main content
15,791,058 members
Articles / Programming Languages / C#

Part 2 of 4: Tips/Tricks for Silverlight Developers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 Nov 2010CPOL3 min read 9.6K   1
Tips/Tricks for Silverlight Developers

Part 1 | Part 2 | Part 3 | Part 4

I wanted to create a series of blog post that gets right to the point and is aimed specifically at Silverlight Developers. The most important things I want this series to answer are:

  • What is it?
  • Why do I care?
  • How do I do it?

I hope that you enjoy this series. Let’s get started:

Tip/Trick #6)

What is it? Create a Notification Window in a Silverlight Out Of Browser Application.

Why do I care? It's a great way to alert users of something that needs their attention. It works similar to the notification you would get with a new email in Outlook.

How do I do it:

  1. Make sure you are developing an OOB application. Right click on your Silverlight Project and make sure there is a check in “Enabled running application out of the browser” as shown below:


  2. Add a button to your page and drop in the following code snippet. This creates a very simple Notification, but you can add borders and more.
    private void button1_Click(object sender, RoutedEventArgs e)
        NotificationWindow notify = new NotificationWindow();
        notify.Height = 100;
        notify.Width = 200;
        TextBlock text = new TextBlock();
        text.Text = "This is the Notification Message";
        text.Foreground = new SolidColorBrush(Colors.Red);
        notify.Content = text;
  3. Here is an example of what it looks like:


Tip/Trick #7)

What is it? You can increase isolated storage if you need more space.

Why do I care? Eventually, you will get a requirement that will call for more space than what is originally allocated by default.

How do I do it? This trick has to come from a Button click Event. So drop a button on the Silverlight Page and paste the following code inside of it.

Note: This code snippet came directly from MSDN.

private void button2_Click(object sender, RoutedEventArgs e)
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        // 5 MB of isolated storage space is needed
        int spaceNeeded = 1024 * 1024 * 5;

        if (store.AvailableFreeSpace < spaceNeeded)
            if (store.IncreaseQuotaTo(store.Quota + spaceNeeded))
                // The user accepted the request

When the user clicks the button, the following dialog will appear. If they click “Yes”, then it will increase Isolated storage.


Tip/Trick #8)

What is it? Use the StringFormat in XAML to format your data (for example, a Birthdate).

Why do I care? It allows you to use standard formatting expressions in XAML rather than put it in code behind.

How do I do it: To format a string into a Birthdate, do the following: add a TextBlock to your main page.

<TextBlock x:Name="txtDOB" Text="{Binding DOB, StringFormat='MM/dd/yyyy'}"/>

Add a class to your project to hold the data:

using System;

namespace SilverlightApplication16
    public class TestClass
        public DateTime DOB { get; set; }

Now add in the code to populate the TextBlock:

TestClass data; 

public MainPage()
    data = new TestClass() { DOB = new DateTime(1979, 5, 1) };
    txtDOB.DataContext = data;

Now when you run the application, your DOB has the following format:


If you do not format the data, it would have looked like this:


Pro Tip: has a lot of great StringFormat examples for DateTime.

Tip/Trick #9)

What is it? A simple way to change the Silverlight Page that starts when your application is launched.

Why do I care? When you are working on a Silverlight application and want to try different tests without messing up your MainPage.xaml or if you want a different startup page to launch when your application is started.

How do I do it: Navigate to your App.xaml.cs and look for Application_Startup. You will want to modify the line highlighted below to point to the new Silverlight Page that you created. Make sure that you comment out the first line that points to MainPage() as shown below:


Tip/Trick #10)

What is it? The GridSplitter control is actually a useful Silverlight control that isn’t talked about very much.

Why do I care? Use the GridSplitter control to provide an easy way to resize rows/columns in a grid at run-time. Depending on the application, your users may like this.

How do I do it: You can accomplish this by adding the following code into a Silverlight Page.

<navigation:Page x:Class="SilverlightApplication16.Page1" 
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page1 Page" 
    <Grid x:Name="LayoutRoot" Background="White">
            <RowDefinition />
            <RowDefinition />

            <ColumnDefinition />
            <ColumnDefinition />

        <Rectangle Fill="#FF2626D8" Margin="37,29,63,21" Stroke="Black"/>
        <Rectangle Fill="#FF52F519" Margin="60,29,40,21" Stroke="Black" Grid.Column="1"/>
        <Rectangle Fill="#FFFF3A00" Margin="37,25,63,25" Stroke="Black" Grid.Row="1"/>
        <Rectangle Fill="#FFE71EB2" Margin="60,25,40,25" Stroke="Black" 
		Grid.Column="1" Grid.Row="1"/>
        <sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Left" 
		Margin="0,-9,0,0" Grid.RowSpan="2" Width="14" Background="Black"/>
        <sdk:GridSplitter Grid.ColumnSpan="2" HorizontalAlignment="Stretch" 
	Height="21" Margin="-12,0,8,-4" VerticalAlignment="Bottom" Background="Black"/>


See the arrows located below? You can resize the grid by clicking on the black space and dragging.

Before dragging:


After dragging:


Thanks for reading and please come back for parts 3 and 4.

alt Subscribe to my feed

<iframe src="" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No> <script language='javascript1.1' src=""></script> <noscript> </noscript> </iframe>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Telerik
United States United States
Michael Crump is a Silverlight MVP and MCPD that has been involved with computers in one way or another for as long as he can remember, but started professionally in 2002. After spending years working as a systems administrator/tech support analyst, Michael branched out and started developing internal utilities that automated repetitive tasks and freed up full-time employees. From there, he was offered a job working at McKesson corporation and has been working with some form of .NET and VB/C# since 2003.

He has worked at Fortune 500 companies where he gained experience in embedded systems design and software development to systems administration and database programming, and everything in between.

His primary focus right now is developing healthcare software solutions using Microsoft .NET technologies. He prefers building infrastructure components, reusable shared libraries and helping companies define, develop and automate process standards and guidelines.

You can read his blog at: or follow him on Twitter at @mbcrump.

Comments and Discussions

GeneralMy vote of 5 Pin
Sandeep Mewara24-Dec-10 20:59
mveSandeep Mewara24-Dec-10 20:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.