我建议使用 MarkupExtension 来完成所有繁重的工作:
public class TranlationExtension : MarkupExtension
{
public string Key { get; set; }
public TranlationExtension(string key)
{
this.Key = key;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
Binding binding = new Binding("TranslationDictionary[" + Key + "]");
binding.Source = MyTranslations;
return binding.ProvideValue(serviceProvider);
}
}
如果文化发生变化,MyTranslations 必须是实现 INotifyPropertyChanged 的类。
用途:
<Button Content="{local:Tranlation TheKeyOfTheTranslationThatIsUsedInTheDictionaryAswell }"/>
这里描述了该方法http://www.wpftutorial.net/LocalizeMarkupExtension.html