Silverlight – Binding ResourceDictionaries

B06C0079 In the last two Silverlight posts I’ve introduced databinding in Silverlight.  We’ve seen that databinding, while similar to what we’ve experienced in Windows Forms and ASP.NET applications, is markedly different in Silverlight.  Today’s topic further illustrates the differences by asserting that you can bind Resources, or more accurately Resource Dictionaries, to your controls.

If you’ve been following along, it really should come as no surprise to you that this is true since all of our databinding examples have actually been automatic retrieval of properties of other classes in our system.

If you’ve already been playing with Silverlight and already done a bit of resource management, you may be wondering what I’m talking about.  After all, if I want to create a resource in a file, I create it using regular XAML, either in the control hierarchy or in the application XAML.  But the truth is, all our XAML is doing is creating objects in the system.  If XAML is creating an object, it should come as no surprise that we can bind to that XAML-created object.

As a matter of practice, I like to define all my resources at the application level because I never know if I’ll need to use the resource in multiple pages and it makes the resources easier to find.

So let’s define our first string resource:

First we’ll need to add a namespace declaration to our application element to tell Silverlight that we want to use classes out of the mscorlib.

<Application xmlns=""

Once we’ve done that, we can add a string resource in the Application.Resources section:

        <clr:String x:Key="First">Joe</clr:String>

And now our page code can reference it using

        <TextBlock x:Name="m_textBlockFirst"
                   Height="Auto" Width="Auto"
                   Text="{StaticResource First}"

This may not be all that exciting as far as code goes, and the truth of the matter is you’d probably never use a string as a Dictionary Resource in your code.  But it does illustrate some of what you can do.

Where this becomes a little more exciting is when you want to create a font or graphic object that can be reused throughout your system.

Since our Application.Resources section can hold ANY object, we could define a Brush object:

        <LinearGradientBrush x:Key="Brush">
            <GradientStop Offset="0.00" Color="Blue" />
            <GradientStop Offset="0.50" Color="Yellow" />
            <GradientStop Offset="1.00" Color="White" />

and then use that by binding to it in our controls as needed:

    <StackPanel Height="Auto" Width="Auto"
                Background="{StaticResource Brush}">

For those of you who thought this post would be addressing the topic of Internationalization, you are probably left wondering how we get from the code above to code that can be swapped out based on what country and region you are viewing the code from.

The good news is, that code is coming.  The bad news is, you can’t get there from here.  The code I’ve shown you is just a way to enable a high degree of code reuse using nothing but XAML.  This will allow us to write applications with fewer lines of code, thereby allowing our Silverlight applications to download in a shorter amount of time.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Silverlight – Wire up your form for programmingSilverlight – Wire up your form for programming As I mentioned a couple of weeks ago, learning Silverlight is a lot more about relearning some basic assumptions than it is about learning a new language.  We've already looked at the […]
  • More on SilverLight Developer InstallationMore on SilverLight Developer Installation If you followed along with the install instructions last week and then went to do some development, you probably noticed that the Visual Studio environment does not allow for WYSIWYG/Drag […]
  • Silverlight – Navigating DataSilverlight – Navigating Data Last week I demonstrated how to access data from a web service in Silverlight and display it on the page.  Today we are going to continue on with that demo and look at how to […]
  • Silverlight – DatabindingSilverlight – Databinding When I was learning DataBinding in both .NET 1.0 and .NET 2.0, I quickly discovered that most of what I learned about DataBinding for ASP.NET was useless as I moved to Windows Forms and […]
  • Installing Silverlight2 Tools for Visual StudioInstalling Silverlight2 Tools for Visual Studio I was talking with a friend of mine a couple of days ago about the future of web development and XAML specifically when he made the comment about the tools not being available for the […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback