【问题标题】:Changing the text field of a button on creation in xaml在 xaml 中更改创建按钮的文本字段
【发布时间】:2015-09-02 18:48:03
【问题描述】:

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

【问题讨论】:

标签: c# xaml


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以将buttoncontent 属性绑定到视图模型中的属性,例如:

    <Button Content="{Binding ButtonTxt}"/>
    

    然后您可以在视图模型中设置ButtonTxt 属性,与所选语言有关,例如

    if (languageId == Languages.English) {
       ButtonTxt = buttonTextInEnglish;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多