Access Localization Resources in WPF from the code-behind using C#

In a previous blog I discussed how to localize a WPF application. In that example we bound the localized resources in the XAML code. However, recently I needed to set the title of a MessageBox and I needed it to be localized.

I added the below to my resource files. The name is the same for each of the resource files, however the value is different per localization – language. English, German and Chinese.

image

From the code-behind, simply reference the Properties class and the name of your property.

MessageBox.Show("The Error Message",
   Properties.Resources.messageBoxTitle,
   MessageBoxButton.OK, MessageBoxImage.Error);

That’s it. We have just added localization to a WPF application from a code-behind. Be sure, like I mentioned above, to check out my other article on this for more details. I also created a video located here, which will walk you through the localization process, step-by-step.