C# 数据绑定组合框中的本地化不是';t工作不正常

C# 数据绑定组合框中的本地化不是';t工作不正常,c#,wpf,mvvm,combobox,converter,C#,Wpf,Mvvm,Combobox,Converter,我想翻译我的组合框中的项目。 所以我使用了一个个性化的converter keytotransationconverter,它将枚举值转换为转换后的字符串 [ValueConversion(typeof(object), typeof(string))] public class KeyToTranslationConverter : IValueConverter { public object Convert(object value, Type targetType,

我想翻译我的组合框中的项目。 所以我使用了一个个性化的converter keytotransationconverter,它将枚举值转换为转换后的字符串

[ValueConversion(typeof(object), typeof(string))]
public class KeyToTranslationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
                                  object parameter, CultureInfo culture)
    {
        return LocalizationResourcesManager.GetTranslatedText(value);
    }
}
我的组合框绑定到可观察的集合LanguagesEntries,selectItem绑定到LanguageEntry属性

<ComboBox ItemsSource="{Binding LanguageEntries}" 
              SelectedItem="{Binding LanguageEntry}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Converter={StaticResource Converter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
将翻译所有项目集合,但所选项目除外,该项目是重复的:

例如,所选项目“Anglais”没有翻译,但单词English在组合框列表中

有人能帮我吗


Arnaud.

我遇到了这个问题,我通过将转换器绑定到itemssource而不是itemtemplate来解决它

<ComboBox ItemsSource="{Binding LanguageEntries, Converter={StaticResource LanguageEntriesConverter}}">

转换需要处理集合而不是每个项:

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is System.Collections.ObjectModel.Collection<string>)
        {
            foreach (var c in (System.Collections.ObjectModel.Collection<string>)value)
            {
                c = LocalizationResourcesManager.GetTranslatedText(c);
            }
        }
        return value;
    }
公共对象转换(对象值、类型targetType、对象参数、字符串语言) { if(值为System.Collections.ObjectModel.Collection) { foreach(var c in(System.Collections.ObjectModel.Collection)值) { c=本地化ResourcesManager.GetTranslatedText(c); } } 返回值; }
每次更新itemssource时都会调用转换器,方法是将其分配给新值或调用OnPropertyChanged。

我遇到了这个问题,我通过将转换器绑定到itemssource而不是itemtemplate来解决它

<ComboBox ItemsSource="{Binding LanguageEntries, Converter={StaticResource LanguageEntriesConverter}}">

转换需要处理集合而不是每个项:

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is System.Collections.ObjectModel.Collection<string>)
        {
            foreach (var c in (System.Collections.ObjectModel.Collection<string>)value)
            {
                c = LocalizationResourcesManager.GetTranslatedText(c);
            }
        }
        return value;
    }
公共对象转换(对象值、类型targetType、对象参数、字符串语言) { if(值为System.Collections.ObjectModel.Collection) { foreach(var c in(System.Collections.ObjectModel.Collection)值) { c=本地化ResourcesManager.GetTranslatedText(c); } } 返回值; }
每次更新itemssource时,都会调用转换器,方法是将其分配给新值或调用OnPropertyChanged。

简单解决方案:不使用转换器,而是在模型上公开LocalizedName属性并绑定到该属性。当然,您的解决方案是有效的。我想使用转换器,因为我的视图模型有很多枚举集合,所以您的解决方案需要太多时间。我认为如果我的视图模型不包含转换代码,则更易于使用。将SelectedItem属性绑定到视图模型的属性,并在更改语言时-刷新绑定的选定属性。刷新视图后,我使用枚举值设置选定属性,并发送PropertyChangedEventHandler事件。IHM获取属性,但未调用转换器,并显示旧的转换值。简单解决方案:不使用转换器,而是在模型上公开LocalizedName属性并绑定到该属性。当然,您的解决方案是有效的。我想使用转换器,因为我的视图模型有很多枚举集合,所以您的解决方案需要太多时间。我认为如果我的视图模型不包含转换代码,则更易于使用。将SelectedItem属性绑定到视图模型的属性,并在更改语言时-刷新绑定的选定属性。刷新视图后,我使用枚举值设置选定属性,并发送PropertyChangedEventHandler事件。IHM获取属性,但未调用转换器,并显示旧的转换值。