正如我们昨天讨论的,如果分组列表可以解决你的问题,例如,你可以这样做:
<Page.Resources>
<CollectionViewSource x:Name="listViewItems" IsSourceGrouped="True" />
<DataTemplate x:Name="listViewItemTemplate">
<TextBlock Text="{Binding BookAddress}" FontSize="20" />
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView x:Name="listView" ItemsSource="{x:Bind listViewItems.View}" ItemTemplate="{StaticResource listViewItemTemplate}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key}" FontSize="25" Foreground="Red" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
后面的代码很清楚:
public MainPage()
{
this.InitializeComponent();
listViewItems.Source = Headers.GetItemsGrouped();
}
我从你的代码中发现你把数据放在一个名为“urls”的字符串List中,我将继续在我的“Headers”类中使用这个List作为数据源,我的“Headers”类也是如此:
public class Headers
{
public string HeaderTitle { get; set; }
public Headers()
{
HeaderTitle = string.Empty;
}
private static List<string> urls = new List<string>
{
"http://favorite.com",
"http://new.com",
"http://feature.com",
"http://favorite.book1.com",
"http://new.book2.com",
"http://feature.book3.com",
"http://favorite.book4.com",
"http://new.book5.com",
};
public static ObservableCollection<BookList> GetCollection()
{
ObservableCollection<BookList> myBookList = new ObservableCollection<BookList>();
foreach (var book in urls)
{
myBookList.Add(new BookList(book));
}
return myBookList;
}
public static ObservableCollection<GroupInfoList> GetItemsGrouped()
{
ObservableCollection<GroupInfoList> groups = new ObservableCollection<GroupInfoList>();
var query = from item in GetCollection()
group item by item.BookAddress[9] into g
orderby g.Key
select new { GroupName = g.Key, Items = g };
foreach (var g in query)
{
GroupInfoList info = new GroupInfoList();
switch (g.GroupName.ToString())
{
case "v":
info.Key = "Favorite";
break;
case "w":
info.Key = "New";
break;
case "a":
info.Key = "Feature";
break;
default:
info.Key = g.GroupName;
break;
}
foreach (var item in g.Items)
{
info.Add(item);
}
groups.Add(info);
}
return groups;
}
}
,也是我BookList
类和GroupInfoList
是这样的:
public class BookList : INotifyPropertyChanged
{
public string _BookAddress;
public string BookAddress
{
get { return _BookAddress; }
set
{
_BookAddress = value;
OnPropertyChanged("BookAddress");
}
}
public BookList(string name)
{
this.BookAddress = name;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class GroupInfoList : List<object>
{
public object Key { get; set; }
}
的BookList
类是为ListView
的项目,如果你想在每一个项目,以显示更多的细节,你可以添加属性这个班。而GroupInfoList
类仅适用于每个组的Key
。
在我的示例,您的URI格式应始终遵循这些模式:
您可以修改Headers
类的GetItemsGrouped()
方法中的代码以满足您的预期模式。
这是该样品的结果:
在你想测试我的样品的情况下,这里是it(Grouped List)。
目前还不清楚你在问什么; 'Category'是'Article'类的一个属性? – levelonehuman
可以说我从网上打三个电话,因为它不是一个API或XML/JSON请求我正在做...我需要按我自己的文章分类。我正在使用多个任务http请求,我有一个3个网址的数组。我想将这3个url的内容添加到一个ObservableCollection中,但仍然能够将所有文章区分为我想要的类别。 – Cody
我认为你应该为'Article'类添加一个'Category'属性。然后,您可以将其分类到您创建每篇文章的位置。这对你想要做什么有意义吗?例如 – levelonehuman