How to get a string resource in C# (WPF)?

Michel Keijzers picture Michel Keijzers · Dec 15, 2012 · Viewed 9.2k times · Source

I know how to get a resource from the string resource in XAML:

{Binding OK, Source={StaticResource LocStrings}}

However, I want to use a localized string in C# directly, e.g. something like

someString = Resources["LocStrings"]["StringId"];

But this does not compile.

Answer

NoOne picture NoOne · Apr 1, 2014

You need something like this:

var myText = this.FindResource("LocStrings") as string;

I guess, you can change this to wherever your resource is located.

UPDATE:

Usually, this is what I use for global resources:

var myText = Application.Current.FindResource("resourceName") as string;