数据绑定组合框中的本地化无法正常工作
我想翻译我的组合框中的项目。 因此,我使用个性化转换器 KeyToTranslationConverter 将枚举值转换为翻译后的字符串。
[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>
我的问题是: 当用户更改语言时,将调用该方法:
CollectionViewSource.GetDefaultView(this.LanguageEntries).Refresh();
除了重复的所选项目之外,所有项目集合都将被翻译:
例如,所选项目“Anglais”不会被翻译,但单词英语位于组合框列表中。
有人可以帮助我吗?
阿诺。
I want to translate items of my combo box.
So I use a personalized converter KeyToTranslationConverter which convert an Enum value to a translated string.
[ValueConversion(typeof(object), typeof(string))]
public class KeyToTranslationConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return LocalizationResourcesManager.GetTranslatedText(value);
}
}
My combo box is bound to the observable collection LanguagesEntries and the selectItem is bound to LanguageEntry attribute.
<ComboBox ItemsSource="{Binding LanguageEntries}"
SelectedItem="{Binding LanguageEntry}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Converter={StaticResource Converter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
My problem is:
When the user change the language the method is called :
CollectionViewSource.GetDefaultView(this.LanguageEntries).Refresh();
All items collections are translated except the selected item which is duplicated :
For example the selected item "Anglais" is not translated but the word English is in the combo box list.
Can someone help me.
Arnaud.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了这个问题,我通过将转换器绑定到 itemssource 而不是 itemtemplate 来解决它。
并且转换需要处理集合而不是每个项目:
每次更新项目源时都会调用转换器,方法是将其分配给新值或调用 OnPropertyChanged。
I had this exact problem, I solved it by binding the converter to the itemssource instead of the itemtemplate.
And the convert need to handle the collection instead of each item:
The converter is called every time you update your itemssource either by assigning it to a new value or calling OnPropertyChanged.