Search code examples
c#wpflocalizationlocalizable.strings

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


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.


Solution

  • 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;