views:

52

answers:

1

Just wondering if it is possible to dynamically create a list of strings in XAML based on language/culture? Say if user logs in as an English user it shows Client Name, Order Number... and if user logs in as a Polish user it shows Nazwa klienta, Numer zamówienia instead?

I only know the hardcoded one like below:

        <System_Collections_Generic:List`1 x:Key="columnNameList">
            <System:String>Client Name</System:String>
            <System:String>Order Number</System:String>
            <System:String>Date</System:String>
        </System_Collections_Generic:List`1>
A: 

I would suggest using a resource file and a markup extension. In the resource file, you create string ressources, and you make localized resource files for each language. In the markup extension, you just return the value of the string from the resources (it will automatically pick up the appropriate language from the satellite resource assembly for the current culture).

Markup extension

[MarkupExtensionReturnType(typeof(string))]
public class ResourceString : MarkupExtension
{
    [ConstructorArgument("resourceKey")]
    public string ResourceKey { get; set; }

    public ResourceString(string resourceKey)
    {
        this.ResourceKey = resourceKey;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        // Assuming your resource file is named StringResources.resx
        return StringResources.ResourceManager.GetString(ResourceKey);
    }
}

XAML

    <local:ListOfStrings x:Key="columnNameList">
        <local:ResourceString ResourceKey="ClientName" />
        <local:ResourceString ResourceKey="OrderNumber" />
        <local:ResourceString ResourceKey="Date" />
    </local:ListOfStrings>

By the way, you can't use generics in XAML (well, you can in XAML 2009, but it's not supported in VS yet). So you need to create a non generic class that represents a list of strings :

public class ListOfStrings : List<string> { }
Thomas Levesque
Thanks for this Thomas, but would this work in Sliverlight? As I know that MarkupExtension is not extensible in Silverlight?
Xin
Oh, I missed the fact you were using Silverlight, I was assuming WPF... You can't create custom markup extensions in Silverlight, sorry...
Thomas Levesque
I will just have to find another way then, still thanks for the help!
Xin