.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to use Static Resource In Silverlight Application

Posted By:Diptimaya Patra       Posted Date: May 23, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

How to use Static Resource In Silverlight Application. Static Resource is nothing but a Resource which is defined and cannot be changed but can be used multiple times.
 

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:


        Hello World

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


      
      

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:

        

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

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
{
  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"




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

Or you can bind the FirstName attribute as follows:


	

	
		

		 
	


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 
{
    public ObjectCollection() 
   {
    }
    public ObjectCollection(IEnumerable collection)
   {
      foreach (object obj in collection) 
     {
        Add(obj);
     }
   }
}


Now in xaml code behind add the Resources as follows:

 
     
        User 1
        User 2
        User 3
     

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

That's it you have successfully used Static Resources.Enjoy Coding.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend