在xaml中更改创建时按钮的文本字段

时间:2015-09-02 18:48:04

标签: c# xaml

我有一个支持多种不同语言的应用程序,因此其中一个要求是用户体验不论语言如何都不会改变。我目前有一个对话框,其中有多个英文按钮,但有没有办法动态更改按钮的文本字段?

2 个答案:

答案 0 :(得分:0)

我建议使用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

答案 1 :(得分:-1)

您可以将content的{​​{1}}属性绑定到viewmodel中的属性,例如:

button

然后,您可以在所选语言的viewmodel方面设置<Button Content="{Binding ButtonTxt}"/> 属性,例如

ButtonTxt