我有一个代表一个人的类,带有一些信息
public class Agente
{
public string title { get; set; }
public string address { get; set; }
public string name { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string website { get; set; }
public string id { get; set; }
public string continent { get; set; }
public string country { get; set; }
public Agente()
{
}
}
我需要将此类的对象分组给其国家,因此我将获得西班牙的PPL列表,这是来自意大利的另一个ppl列表。有了这些列表后,我需要在Windows Phone应用程序上显示它们:
国家
人1
人2
国家
人3
人4
最好的方法是什么?
您需要通过人名单迭代,然后按国家名称订购。
List persons..
HashTable<List<Person>> countries=new HashTable();
foreach(person in persons){
if(countries.contains(person.country)){
countries.value.add(person);
}
else{
List<Person> per = new list();
per.add(person);
countries.add(person.country,per);
}
}
假设您已经以某种顺序有它们:
列表项目= ....
var byCountry = items.GroupBy( agente => agente.country);
或按国家订购它们,然后按ID订购:
var byCountry = items.OrderByBy(agente => agente.country).ThenBy(agente => agente.id);
要在WPF中渲染这一点,您需要某种集合。我假设您正在使用ObservableCollection,因此在您的视图上,请确保您拥有合适的属性,例如:
public ObservableCollection<Agente> Agents{get; private set;}
并这样初始化:
var byCountry = items.OrderByBy(agente => agente.country).ThenBy(agente => agente.id);
this.Agents = new ObservableCollection<Agente>(byCountry);
现在,在您的WPF中,声明您的ItemControl
绑定到此集合:
<ItemsControl x:Name="Agents" ItemsSource="{Binding Agents}>
注意:这假设您已将视图模型绑定到包含ItemsControl
的项目。