I need TextBlock.Text to be retrieved from translation manager, something like
<TextBlock Text="{Binding TranslateManager.Translate('word')}" />
I don’t want to set DataSource for all text blocks. The only way I found how to do this is to bind to “static” class and use converter:
<TextBlock Text="{Binding Value,
Source={StaticResource Translation},
Converter={StaticResource Translation},
ConverterParameter=NewProject}" />
And these helper class
public class TranslationManager : IValueConverter
{
public static string Translate(string word)
{
return translate(word);
}
// this is dummy for fake static binding
public string Value { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var name = parameter as string;
return TranslationManager.Translate(name, name);
}
}
But, is there a better – shorter – way?
Let me prefix this by stating: You should be using static resources for translating words: Application Resources or *.RESX Files
However, if you need to simplify your xaml, the only thing you are missing is placing a datacontext on your entire view. It sounds like you are not using MVVM, so placing this logic in the constructor or your code behind gives you access to more features through binding:
Then, in your xaml, your textboxes can simplify to this:
My Translator: