Karteek
Karteek

Reputation: 79

Localizing in WPF

I am localizing a WPF application. The datagrid columns headers needs to be changed at runtime according to the specific UI culture in the XAML.

Though I am able to do the localization in the code behind and also in XAML by using the LocBaml tool. I am not able to achieve it with one particular scenario in the XAML. The scenario is that I am parsing XAML, as I am loading it in an XML file and parsing..

So now when I parse it like:

<dg:DataGridColumnHeader Header="{x:Static findlocale:My.Resources.String.anylocalword}"></dg:DataGridColumnHeader>

Where findlocale is the XAML namespace keyword, I am getting an error:

XAML parse error. Cannot find type My.Resources.String.localword in the xaml namespace.

Why is this not working in this scenario? How do I overcome it?

Upvotes: 3

Views: 484

Answers (1)

CodeNaked
CodeNaked

Reputation: 41403

I'm willing to bet your findlocale XML namespace is incorrect. If you have something like:

namespace MyNameSpace.MySubNamespace {
    public class MyClass {
        public static string MyProperty { get; set; }
    }
}

Then your XML namespace must be declared like xmlns:findlocale="clr-namespace:MyNameSpace.MySubNamespace" in order to use {x:Static findlocale:MyClass.MyProperty}.

You cannot declare your XML namespace like xmlns:findlocale="clr-namespace:MyNameSpace" and use it like {x:Static findlocale:MySubNamespace.MyClass.MyProperty}.

Also, if you have any nested classes/enums, then you must use a + sign in place of the .. So if you had:

namespace MyNameSpace.MySubNamespace {
    public class MyClass {
        public class MyNestedClass {
            public static string MyProperty { get; set; }
        }
    }
}

To access MyProperty, you'd need to declare your XML namespace like xmlns:findlocale="clr-namespace:MyNameSpace.MySubNamespace" and access it like {x:Static findlocale:MyClass+MyNestedClass.MyProperty}.

Upvotes: 2

Related Questions