【发布时间】:2014-03-28 12:33:02
【问题描述】:
我正在尝试设置多语言应用程序,因此当用户更改显示语言时,所有打开的窗口中的所有文本都会自动更改。 我在绑定组合框控件时遇到问题。绑定需要在代码隐藏中完成,因为我有来自数据库的动态内容,有时我什至必须在运行时创建额外的组合框。 此外,我不想将翻译保留在数据库中,因为我不想每次用户更改显示语言时都查询数据库。 到现在为止我做了什么:
在xaml:
<ComboBox x:Name="cmb"/>
在C#:
public class MyCmbItem
{
public int Index { get; set; }
public string Text { get; set; }
}
private ObservableCollection<MyCmbItem> LoadText()
{
ObservableCollection<MyCmbItem> _result = new ObservableCollection<MyCmbItem>();
foreach (var _item in _list)
{
//the list is coming from a database read
_result.Add(new MyCmbItem { Index = _item.Value, Text = _res_man_global.GetString(_item.KeyText, _culture) });
}
return _result;
}
public ObservableCollection<MyCmbItem> MyTexts
{
get { return LoadText(); }
set {} //I do not have to add/remove items at runtime so for now I leave this empty
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
...
LoadList(); //this adds values in _list
cmb.ItemsSource = MyTexts; //this populates the combo-box
在这里我卡住了,我不知道如何确定组合框来刷新显示的文本。该方法必须实现,如果我打开了几个窗口,每个窗口都包含随机数量的组合框,当我更改当前语言时,所有窗口中的所有组合框都将刷新显示的列表,而不会影响内部的其他值(如选定的项目)。有人知道这是怎么做到的吗?
非常感谢。
【问题讨论】:
-
在应用程序运行时处理语言更改是否至关重要?更改语言通常是极其罕见的情况,那么是否值得努力开发功能来处理这种极端情况?相反,您能否通过简单地提示用户关闭并重新启动应用程序来响应语言更改?
标签: c# wpf binding combobox multilingual