tags:

views:

87

answers:

1

Hi,

I'm a beginner in WPF and currently working on my first app. For this project I'm supposed to use this method to add localization in the app:

[TranslationService.cs]

public class TranslationService
{
        string language;


        public TranslationService(string language)
        {
            this.language = language;
        }

    public string GetTranslation(string key)
        {
            var value = *select the value from database based on "key" and "language"*
        return value;
        }
}

I'm trying to figure out how I can use this GetTranslation method in XAML in an elegant way.. but I'm having a hard time.

I'm after something like this:

[SomePage.xaml]

..
<TextBlock Text="{Binding source="_translator" parameter="WelcomeMessage" }" />
..

I reallly tried figuring it out myself using the 2 WPF books I have and some googling.. but I get lost in the new syntax..

Who can help me get back on track?

A: 

If you use MVVM pattern, you can put this logic into your ViewModel:

class MyViewModel
{
    private readonly TranslationService translator_;

    public string WelcomeMessageText
    {
        get { return translator_.GetTranslation("WelcomeMessage"); }
    }

    // ...
}

And bind to that in XAML:

<TextBlock Text="{Binding WelcomeMessageText}"/>
Bojan Resnik