昨日議論してきたように、グループ化されたリストはあなたの問題を解決することができれば、あなたはこのような例のためにそれを行うことができます:
<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」という名前の文字列リストにデータを配置していることがわかります。このリストを「Headers」クラスのデータソースとして使用し続けます。このように、「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
のクラスです。私のサンプルで
、あなたのウリの形式は、常にこれらのパターンに従ってください:
君はGetItemsGrouped()
メソッドのコードをHeaders
クラスに変更して、期待どおりのパターンに合わせることができます。
これは、このサンプルの結果である:あなたは私のサンプルをテストしたい場合には

、ここit(Grouped List)です。
あなたが尋ねていることは不明です。 'Category'は' Article'クラスのプロパティですか? – levelonehuman
私はWebから3回の呼び出しをしていると言いますが、それはAPIまたはXML/JSON要求ではないため、私は自分の記事でカテゴリを分類する必要があります。私は複数のタスクのHTTP要求を使用して、私は3つのURLの配列を持っています。私は1つのObservableCollectionにこれらの3つのURLのコンテンツを追加したいが、それでもそれらを必要とするカテゴリにすべての記事を区別することができる。 – Cody
'Article'クラスに' Category'プロパティを追加するべきだと思います。次に、各記事を作成する場所を分類することができます。あなたは何をしようとしているのですか? – levelonehuman