简体   繁体   English

多语言WPF应用程序:不能与组合框一起使用

[英]Multilingual WPF Application: not working with combobox

I have created a multilingual WPF application using Andre's answer from here . 我创建使用安德烈的回答从一个多语种的WPF应用程序在这里 I'm binding text like this 我这样绑定文本

<TextBlock Text="{DynamicResource Create}"/>

and can switch from english to french at runtime - nice! 并且可以在运行时从英语切换到法语-太好了! However, this does not work with ItemsSource. 但是,这不适用于ItemsSource。 For example, I have a ComboBox that should display all available languages: 例如,我有一个组合框,应显示所有可用的语言:

<ComboBox ItemsSource="{Binding AllLanguages, Source={StaticResource Locator}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{DynamicResource LanguageId}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Set up like this, the combobox displays no text. 像这样设置,组合框不显示任何文本。 If I set the textblock's text inside to Text={LanguageId} , I see the LanguageIds 'eng', 'fr' etc., so the binding works. 如果我将文本块的文本设置为Text={LanguageId} ,则会看到LanguageIds'eng','fr'等,因此绑定有效。

When using a converter: 使用转换器时:

<TextBlock Text="{Binding LanguageId, Converter={StaticResource DynamicResourceConverter}"/>

languages are displayed as "English" and "French". 语言显示为“英语”和“法语”。 When I switch the language, however, the converter is not called again and the language names are not updated - so that is no real workaround. 但是,当我切换语言时,不会再次调用该转换器,并且语言名称也不会更新-因此这不是真正的解决方法。

I'd be very thankful for a tip on the cause and how to fix this. 对于原因和解决方法的提示,我将非常感谢。

I'll explain first why a few things are not working. 我将首先解释为什么有些事情不起作用。

    ....
    <DataTemplate>
        <TextBlock Text="{DynamicResource LanguageId}"/>
    </DataTemplate>
    ....

This is a short hand for Text="{DynamicResource ResourceKey='LanguageId'}" which is a static string literal and does not involve any binding. 这是Text="{DynamicResource ResourceKey='LanguageId'}"的简写,它是静态字符串文字,不涉及任何绑定。

It would be great if the following was available, but unfortunetly is NOT POSSIBLE because the target for the binding is not a DependancyProperty . 这将是巨大的,如果下面是可用的,但偏偏是不可能的 ,因为绑定的目标不是DependancyProperty

    ....
    <DataTemplate>
        <TextBlock Text="{DynamicResource ResourceKey={Binding LanguageId}}"/>
    </DataTemplate>
    ....

You are close with your workaround. 您已接近解决方法。 My suggestion would be to try the following:- 我的建议是尝试以下方法:

    ....
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource DynamicResourceConverter}">
                    <Binding Path="LanguageId"/>
                    <Binding Path="SomeOtherPropertyThatChangesWhenLanguageIsSwitched" Source="{StaticResource Locator}"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
    ....

You will need to expand the DynamicResourceConverter to now implement also IMultiValueConverter . 您将需要扩展DynamicResourceConverter以现在也实现IMultiValueConverter In a MultiBinding scenario, if either bound expression changes then the converter is called again. MultiBinding方案中,如果任一绑定表达式发生更改,则将再次调用该转换器。 You would write the Converter such that it only operates on values[0] of the supplied object array as the second value is not needed and only provided a trigger for the converter to be called again. 您将编写Converter ,使其仅在提供的对象数组的values[0]上操作,因为不需要第二个值,并且只为再次调用该转换器提供了触发器。

    public class DynamicResourceConverter: IValueConverter, IMultiValueConverter
    {
        ....
        // original converter implementation for IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ....
        }

        // newly added converter implementation for IMultiValueConverter
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //call the original converter method with one value (assuming you've checked the array has at least one item!!
            return Convert(values[0], targetType, parameter, culture)
        }

        ....
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM