简体   繁体   English

从语言文件绑定动态值的正确方法是什么?

[英]What is the proper way to bind a dynamic value from a language file?

I have a view model which has the name of an entry in my language resource file. 我有一个视图模型,在我的语言资源文件中有一个条目的名称。
I tried to bind this value directly to the x:Uid attribute of the TextBlock in my XAML, but got a XAML error. 我试图将此值直接绑定到我的XAML中的TextBlock的x:Uid属性,但是出现了XAML错误。

To get around this limitation I though about changing the property to return the value from the language file, but was concerned that doing so might not be a valid MVVM solution. 为了解决这个限制,我想改变属性以从语言文件中返回值,但是担心这样做可能不是有效的MVVM解决方案。
I also thought about creating a converter to set the text. 我还想过创建一个转换器来设置文本。

Way that does not work: 不起作用的方式:

<StackPanel Orientation="Horizontal">
    <Image Margin="0,0,20,0" Source="{Binding IconPath}" />
    <TextBlock x:Uid="{Binding LanguageResourceName}" />
</StackPanel>

The view model I am binding to: 我绑定的视图模型:

class Tab : ViewModelBase
{
    private string _IconPath,
        _LanguageResourceName;
    private ViewModelBase _ViewModel;

    /// <summary>
    /// The path to the icon to show on the tab.
    /// </summary>
    public string IconPath
    {
        get { return _IconPath; }
        set { SetProperty(ref _IconPath, value); }
    }

    /// <summary>
    /// The name of the entry in the language resource file to display on the tab.
    /// </summary>
    public string LanguageResourceName
    {
        get { return _LanguageResourceName; }
        set { SetProperty(ref _LanguageResourceName, value); }
    }

    /// <summary>
    /// The contents of the tab.
    /// </summary>
    public ViewModelBase ViewModel
    {
        get { return _ViewModel; }
        set { SetProperty(ref _ViewModel, value); }
    }
}

So what is the correct way to resolve this issue? 那么解决这个问题的正确方法是什么?

Converter is the best way to do it. 转换器是最好的方法。 Check out my answer here for a quick explanation. 在这里查看我的答案以便快速解释。 I've copied the Converter I define there below. 我已经复制了我在下面定义的转换器。

ResourceController is a simple Controller which gets a reference to a ResourceLoader and retrieves values via a method GetString(string resourceId) . ResourceController是一个简单的Controller,它获取对ResourceLoader的引用,并通过方法GetString(string resourceId)检索值。

public class ResourceTranslationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var valString = value as string;

        // If what is being converted is a string, return the resource translation
        // Else return something else, such as the object itself
        return valString == null ? value : ResourceController.GetString(valString);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Binding then works like: 绑定然后工作如下:

<TextBlock Text="{Binding LanguageResourceName, Converter={StaticResource ResourceTranslationConverter}}" />

Make sure you've defined an accessible ResourceTranslationConverter . 确保您已定义可访问的ResourceTranslationConverter Possibly in the Page.Resources or even in your App.xaml (since you should only need one static reference). 可能在Page.Resources ,甚至在App.xaml (因为您只需要一个静态引用)。

Hope this helps and happy coding! 希望这有助于编码!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定到值的正确方法是什么,同时在该项目上定义的预设列表中显示友好名称? - What is the proper way to bind to a value while showing a friendly name from a list of presets also defined on that item? 在没有无限浮动值的情况下将百分比值从平面文件保存到数据库表的正确方法是什么? - What's the proper way to save percentage value from flat file to database table without infinite floating values? 从单个嵌套的XElement中检索值的正确方法是什么? - What is the proper way to retrieve a value from a single, nested, XElement? 将数据从文件保存到对象C#的正确方法是什么 - What is proper way to save data from file to object C# 在MVVM / MVVM Light中绑定单选按钮的正确方法是什么 - What is the proper way to bind a radio button in MVVM / MVVM Light WPF MVVM中将FrameworkElement事件绑定到视图模型处理程序的“正确”方法是什么? - What is the “proper” way in WPF MVVM to bind a frameworkElement event to a viewmodel handler? 在树视图中指定数据上下文并绑定分层数据的正确方法是什么? - What is the proper way to designate the data context and bind hierarchical data in the treeview? 在内部访问值的正确方法是什么? - What is the proper way to access a value internally? 插入数值的正确方法是什么 - What is the proper way to insert a numeric value 加密XmlTextWriter并将其序列化为文件的正确方法是什么? - What is the proper way to encrypt an XmlTextWriter and serialize it to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM