[英]c# wpf multiple dictionaries to datagrid
I have multiple dictionaries. 我有多本字典。 Each of them contains the same keys, just with different values. 它们每个都包含相同的键,只是值不同。 I am using them for translations. 我正在使用它们进行翻译。 They are dynamically created.. 它们是动态创建的。
So, it looks something like this: 因此,它看起来像这样:
DictionaryEng: first_page_name = "First page" second_page_name = "Second page" DictionaryEng:first_page_name =“第一页” second_page_name =“第二页”
DictionaryRu: first_page_name = "Первая страница" second_page_name = "Вторая страница" DictionaryRu:first_page_name =“Перваястраница” second_page_name =“Втораястраница”
I would like this informations to be shown in DataGrid or, so user can change the values. 我希望此信息显示在DataGrid中,以便用户可以更改值。 It should look like this , end values to be editable. 它应该看起来像这样 ,结束值是可编辑的。
What's the best practice to do this? 最佳做法是什么?
Basically you need to bind the DataGrid to a Collection 基本上,您需要将DataGrid绑定到集合
<DataGrid
ItemsSource="{Binding Lines}" AutoGenerateColumns="True"
I'll mock up the creation of the dictionary with stub functions called in the ViewModel c.tor 我将使用在ViewModel c.tor中调用的存根函数模拟字典的创建
public class MyVM : ViewModelBase
{
public MyVM()
{
Line.DictionaryEng = Line.DictionaryEngStub();
Line.DictionaryRu = Line.DictionaryRuStub();
lines = new ObservableCollection<Line>(Line.DictionaryEng.Keys.Select(k => new Line() { KeyWord = k }));
}
private ObservableCollection<Line> lines;
public ObservableCollection<Line> Lines
{
get { return lines; }
set
{
lines = value;
OnPropertyChanged("Lines");
}
}
}
where the underlying class is defined as follows 基础类的定义如下
public class Line : ViewModelBase
{
internal static Dictionary<string, string> DictionaryEngStub()
{
return new Dictionary<string, string>()
{
{ "first_page_name ","First page" },
{ "second_page_name ","Second page" }
};
}
internal static Dictionary<string, string> DictionaryRuStub()
{
return new Dictionary<string, string>()
{
{"first_page_name ","Первая страница" },
{"second_page_name ","Вторая страница" }
};
}
internal static Dictionary<string, string> DictionaryEng = new Dictionary<string, string>();
internal static Dictionary<string, string> DictionaryRu = new Dictionary<string, string>();
private string keyWord;
public string KeyWord
{
get { return keyWord; }
set
{
keyWord = value;
OnPropertyChanged("keyWord");
}
}
public string EnglishWord {
get
{
string english;
if (DictionaryEng.TryGetValue(keyWord ?? "", out english))
{
return english;
}
return null;
}
}
public string RussianhWord
{
get
{
string russian;
if (DictionaryRu.TryGetValue(keyWord ?? "", out russian))
{
return russian;
}
return null;
}
}
}
Notice that the translations have only a getter to retrieve the string from the dictionary. 注意,翻译只有一个getter来从字典中检索字符串。 You can easily make them editable by adding a setter to save the new translation into a persistence layer. 您可以通过添加设置器将新翻译保存到持久层中来轻松地使它们可编辑。 Furthermore, English and Russian dictionaries are generic enough to be renamed as from/to dictionaries. 此外,英文和俄文字典具有足够的通用性,可以重命名为“自/至”字典。 Once the user selects a language in another combobox, you can reset the dictionary accordingly. 用户在另一个组合框中选择一种语言后,您可以相应地重设词典。 Since I have a stub here, the setter does not make much sense, but just to give you and idea... 既然我在这里有一个存根,设置员就没有多大意义,而只是给你和想法...
private string englishSaved;
public string EnglishWord {
get
{
if (englishSaved != null)
{
return englishSaved;
}
string english;
if (DictionaryEng.TryGetValue(keyWord ?? "", out english))
{
return english;
}
return null;
}
set
{
englishSaved = value; //save the new translation into a persistence layer
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.