【发布时间】:2014-10-20 02:20:07
【问题描述】:
如果我一直在正确地研究这个,我之前得到了一些帮助,一位用户说最好使用Dictionary 来存储我的Country 和Places。
所以我创建了我的Dictionary:
Dictionary<string, NewCountryClass> NTCD = new Dictionary<string, NewcountryClass>();
当用户单击按钮时,它将触发此类,我希望它在运行时在Dictionary 内创建newCountryClass 的实例。它将添加字符串,即newCountryTitle.Country,然后是Class。
public void AddCountryCollection()
{
newCountryClass = new NewCountryClass(newCountry,"");
Collections.Add(newCountryClass);
NTCD.Add(newCountryClass.Country, newCountryClass);
}
假设用户添加了Country,它在运行时创建了这个Dictionary,他们添加了4个Countries,但现在想返回并在第二个内添加Place标签国家/地区。
这是newCountryClass:
private string _country;
public string Country
{
get { return _country; }
set
{
if (_country.Equals(value))
return;
_country= value;
RaisePropertyChanged(() => Country);
}
}
private ICollection<string> _places;
public ICollection<string> Places
{
get
{
if (_places== null)
_places= new ObservableCollection<string>();
return _places;
}
set
{
if (value == _places)
return;
_places= value;
RaisePropertyChanged(() => Places);
}
}
如果他们创建了 4,并且他们想将 Place 添加到他们创建的 第二个 的 Country 内的列表中,我该如何找到它?
【问题讨论】:
-
考虑使用 OrderedDictionary ,看起来这是唯一的方法。然而,它不是通用的。性能可能比普通的通用字典稍差。
-
这个问题也不应该这么长。它应该只有几行。为什么?问题只是如何获取字典的第二个元素。就这样。提出这样的问题会使许多用户(包括专家)忽略这个问题(我们通常害怕长问题)。
标签: c# wpf class dictionary