WPF: What is a Resource?

Posted on 2/13/2007 @ 4:18 PM in #Silverlight and WPF by | Feedback | 7556 views

So what is a resource? Is it something in your project, whose properties has something called "Build Action", and you change that to "Embedded Resource" and then you don't need to ship 20 files with your DLL? Yeah, that is an embedded resource, and I've talked previously on my blog about how to use that.

That is not what this blog post is about however. This is about a "Resource in WPF".

What is a resource in WPF?

A resource in WPF is a quick and easy way to re-use commonly defined objects and values over your XAML or in general WPF code. Here is an example. Say for instance, you were writing a XAML flow document as follows

<FlowDocument FontSize="14" FontFamily="Georgia">
  <Paragraph>
    <TextBlock>
      All about Resources in WPF
    </TextBlock>
    <TextBlock>
      This blogpost talks about what resources are in WPF.
    </TextBlock>
    <TextBlock>
      What this blog post doesn't talk about however, is databinding.
    </TextBlock>
  </Paragraph>
</FlowDocument>

When you render the above in a suitable container, the three textblocks look exactly the same. This can be seen as below:

But from the content, it is clear that the first textblock needs to be formatted differently - afterall, it is a heading. So you could suitably add font, color etc. to the individual tags, but in C# you have the flexibility of declaring constants or statics in a class, and use them all over your code. By isolating such variables at a central and easy to maintain place, you make your code easier to use.

Resources are almost on the same theme. They let you isolate and re-use commonly defined objects and values. To view this in action, just modify the above XAML to include resources like this -

<FlowDocument FontSize="14" FontFamily="Georgia">
  <FlowDocument.Resources>
    <SolidColorBrush x:Key="Heading" Color="Red"/>
    <SolidColorBrush x:Key="Content" Color="Blue"/>
  </FlowDocument.Resources>
  <Paragraph>
    <TextBlock Foreground="{StaticResource Heading}">
      All about Resources in WPF
    </TextBlock>
    <TextBlock Foreground="{StaticResource Content}">
      This blogpost talks about what resources are in WPF.
    </TextBlock>
    <TextBlock Foreground="{StaticResource Content}">
      What this blog post doesn't talk about however, is databinding.
    </TextBlock>
  </Paragraph>
</FlowDocument>

This, will identify the heading somewhat differently in Red color as shown below:

As you can see, now if I wished to change all my content from "Blue" to "Green", I'd have to make only one change to my XAML file, and that would be -

<SolidColorBrush x:Key="Content" Color="Green"/>

Here is the resultant Window:

Clearly, this alone isn't that useful. You probably want to set both background, and foreground, maybe even font size, or even other things (3d effects?) that will delineate one part of your XAML from another. You can group these resources in a "Style" and get functionality a.k.a. Stylesheets in XAML.

Sound off but keep it civil:

Older comments..


On 6/5/2008 12:25:55 AM proxyvon said ..
Nice one...


On 3/4/2009 12:50:50 AM Dheeraj Gupta said ..
Wow,


It really hellped me a lot o understand resources and also provide proper hint to get idea about what the styles can do and when they are required.


Please, keep me updated with all your posts related to WPF.

Thanks and Regards


Dheeraj Gupta


On 10/26/2009 12:38:44 AM vivek garg said ..
good one...really helped me to understand the basics of resources


On 5/21/2010 9:17:02 AM mehmet said ..
Thanks a lot for preparing such a good site.


On 6/18/2010 10:27:17 AM Jenny said ..
Thank you so much! This really helped me understand it. Please keep up the good work!! Your blog is one of the first blogs that made it click.