Reader Level:
Articles

Using Static Resource in Silverlight 3 Application

By Diptimaya Patra on August 18, 2009
This article shows how to use static resource in Silverlight 3.
  • 0
  • 0
  • 59888


Introduction

In this article we will discuss more about using and managing Static Resource in Silverlight 3.

Static Resource is nothing but a Resource which is defined and cannot be changed but can be used multiple times.

Steps to Bind Static Resource

  1. Define the respective Namespaces
  2. Define the instance in ResourceDictionary and give a name to it's x:key
  3. To Bind to it, use the {StaticResource} markup extension.

Binding to a Static Resource of Simple String

Create a Silverlight Application and add two TextBoxes.

In the xaml code behind add the namespace:

xmlns:sys
="clr-namespace:System;assembly=mscorlib"

Now add a String resource as described below:

<
UserControl.Resources>
        <sys:String x:Key="SingleString">Hello World</sys:String>
</
UserControl.Resources>

As we discussed earlier it can be used multiple number of times. We will use it in both TextBoxes:

<
Grid>
      <TextBlock Margin="0,0"
                 Text="{Binding Source={StaticResource SingleString}}" />
      <TextBlock  Margin="0,16" Text="{Binding Source={StaticResource SingleString}, Path=Length}"/>
</
Grid>

Path is an attribute using which we can refer to a property of Static Resource.

Binding to a Static Resource of a single instance of a Custom Class

Create a Silverlight Application and add a class; name it as Users.cs.

Now define the class using the following properties:

public
class Users

{

  public string FirstName { get; set; }

  public string LastName { get; set; }}

To use it in xaml first define the namespace:

xmlns:local
="clr-namespace:StaticResourceSL3"

And then add the resources as follows:

<
UserControl.Resources>
       
<local:Users x:Key="SingleUser" FirstName="Diptimaya" LastName="Patra"/>
</
UserControl.Resources>

To bind it to follow the earlier instructions and use Path attribute to use the property:

<
TextBlock Text="{Binding Source={StaticResource SingleUser}, Path=FirstName}"/>

Binding to a Static Resource of a Collection of Custom Instances

We will follow the above example i.e. we will use the same class Users.cs. We will add another class called UserList which will inherit List of Users. As follows:

public
class Users

{

  public string FirstName { get; set; }

  public string LastName { get; set; }

}

public class UsersList : List<Users>

{

  public UsersList

 {

   this.Add(new Users() { FirstName = "John", LastName = "Lock"});

   this.Add(new Users() { FirstName = "James", LastName = "Soyer"});

   this.Add(new Users() { FirstName = "Jack", LastName = "Sephered"});

 }}

As usual add the namespace to have a reference of the class.

Now add the Resources to xaml code behind:

Before that refer to this assembly.

xmlns:controls
="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"

<UserControl.Resources>

    <local:UsersList x:Key="UsersList"/></UserControl.Resources>

Now bind the Static Resource to a ListBox's ItemSource as follows:

<
ListBox  Margin="0,135,0,50" ItemsSource="{StaticResource UsersList }"/>

Or you can bind the FirstName attribute as follows:

<
ListBox  Margin="0,60,0,120" >

<ListBox.ItemsSource>

            <Binding Source="{StaticResource somePersons}" />

</ListBox.ItemsSource> 
            <ListBox.ItemTemplate> 
                        <DataTemplate>
<TextBlock Text="{Binding Path=FirstName}"></TextBlock> 
                         </DataTemplate> 
            </ListBox.ItemTemplate> 
</ListBox>

Binding to a Static Resource of a Collection of Strings

Now assume that you need a collection of strings to be added to a ListBox or to a AutoCompleteBox, how will you do it:

Create a class and name it as ObjectCollection and add the following constructors:

public
partial class ObjectCollection : Collection<object>

{
    public ObjectCollection() 
   {
    }
    public ObjectCollection(IEnumerable collection)
   {
      foreach (object obj in collection) 
     {
        Add(obj);
     }
   }
}

Now in xaml code behind add the Resources as follows:

<
UserControl.Resources>
     <controls:ObjectCollection x:Key="SampleData">
        <sys:String>User 1</sys:String>
        <sys:String>User 2</sys:String>
        <sys:String>User 3</sys:String>
     </controls:ObjectCollection>
  </UserControl.Resources>

Now you can use it as the ItemSource of a ListBox or AutoCompleteBox or others:

<
ListBox  Margin="0,135,0,50" ItemsSource="{StaticResource SampleData}"/>

That's it you have successfully used Static Resources.

Enjoy Coding.

Article Extensions
Contents added by Babak Sekandari on May 20, 2011
Shouldn't "somePersons" be UserList ?
Contents added by Babak Sekandari on May 20, 2011
Shouldn't <local:UserList actually be <controls:UserList since the xmlns is "controls?"
Diptimaya Patra

Visit in http://diptimayapatra.wordpress.com for latest articles.Facebook Page: https://www.facebook.com/MethodOfTried

COMMENT USING

Trending up