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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:clr="clr-namespace:System;assembly=mscorlib"
  x:Class="SilverlightApplication1.App"
  >

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

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

And now our page code can reference it using

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

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" />
        </LinearGradientBrush>

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.

Related Post

One Response to “Silverlight – Binding ResourceDictionaries”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear