Press "Enter" to skip to content

Tag: WPF

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”:

xmlns:resources="clr-namespace:ScottsOpenSourceRPG.Resources"

 

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

Title="{x:Static resources:Literals.About}"

 

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”):

xmlns:mytext="clr-namespace:ScottsOpenSourceRPG.Resources"
Title="{x:Static mytext:Literals.About}"

 

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

<Window x:Class="ScottsOpenSourceRPG.About"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:resources="clr-namespace:ScottsOpenSourceRPG.Resources"
       Title="{x:Static resources:Literals.About}"
       FontSize="11pt"
       WindowStyle="ToolWindow"
       WindowStartupLocation="CenterOwner"
       ResizeMode="NoResize"
       Width="400" Height="200">

 

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 Comments