How to bind a string from a resource file to a WPF/XAML window

One of my personal projects is a C# open-source RPG engine, with a WPF front-end. I want it to be usable by people in different countries, in different languages.

So, text in the UI will use strings from resource files. This will make it easy to internationalize the program.

These are the steps to bind a value from a resource file to a property of a WPF window.

 

Create the resource file

I created a “Resources” folder, in the WPF project, to hold all the resources files.

Next, I created a resource file named “Literals.resx”, to hold all the screen literals – window titles, labels, error messages, etc.

IMPORTANT: Make your resource files public, if you want to bind their values to a WPF UI. Even though the resource file is inside the WPF project, and “internal” should work (based on the normal rules of scope), you need to make the resource file “public”, in order for the data-binding to work.

For this demonstration, I wanted to bind the word “About” to the “Title” property of an About window. So, I created a literal with the name, and value, of “About”.

Resource file, with AccessModifier set to Public

 

Then, I added a new WPF Window, named “About.xaml”, to the WPF project.

WPF project, with Resource folder, holding Literals.resx

Bind the resource value to the WPF window’s property

The next step was to add the namespace for the resource file to my WPF window.

I added this to the opening “Window” tag, and named the XML namespace “resources”. Its value is the project namespace, plus the namespace created by my “Resources” folder. So, that attribute looks like this (for my project “ScottsOpenSourceRPG”:

 

To bind the “About” value, from the resource file, to the window’s Title attribute, I added this line:

 

You don’t have to name your name space “resources”. But, whatever you name it, you will need to use that same name in the binding for the Title attribute. So, this would also work (using the name “mytext”, instead of “resources”):

 

Here is the completed Window tag, with a few other attributes set for the window’s size, location, etc.:

 

Summary

It takes a little extra work to prepare your program for internationalization. However, especially with open-source projects, I believe it’s worth the effort.

Once this data-binding is in place, it will be simple to have someone translate the text in the resource file into another language.

6 thoughts on “How to bind a string from a resource file to a WPF/XAML window

  1. Thanks for your tutoral 🙂

    How could I add a second resx file for another language and then be able to switch the lookup between the two resx files?

  2. You have beautifully explained each step.

    I am making an ERP application having many controls and a number of user-controls (for forms)
    I want to bind the content and label of each control (like textblock, buttons etc.) to strings in resource files.

    Could you please tell me what is the cost of this resource binding in terms of memory and processing power vis-à-vis not using binding ?

    Thanks

    1. Thank you.

      I have not seen any performance or memory problems when using resource files. They might be difficult to manage, if you have thousands of of strings, and dozens of different languages. In that situation, you might want to use a database with a separate program to make it easier to create string values for another language (or editing existing values). But, for a smaller number of strings, it should work as fast as using a class with public fields or constants.

Leave a Reply

Your email address will not be published. Required fields are marked *