积极答复者
使用group GridView分组,拿到数据都是挤在一块的,分不了组(C++)

问题
-
有点多,请耐心看完。
1,首先我的数据结构是这样定义的 homedata.h
namespace TestWin8 { [Windows::UI::Xaml::Data::Bindable] public ref class HomeData sealed { property Platform::String^ imgurl; // 图片地址 property Platform::String^ title1; // 电影名字 }; [Windows::UI::Xaml::Data::Bindable] public ref class DataBox sealed { public: property Platform::String^ catename; //分类名称:科幻/战争/恐怖....等 private: Platform::Collections::Vector<HomeData^>^ m_items; public: property Windows::Foundation::Collections::IVector<HomeData^>^ ite //数据集合 { Windows::Foundation::Collections::IVector<HomeData^>^ get() {return m_items; } } public: DataBox(void) { m_items = ref new Platform::Collections::Vector<HomeData^>(); //构造函数 } }; }
2,数据请求:
void GroupedItemsPage::GetData() { String^ strData= commModule->GetResponsString(); JsonValue^ jsonValue = JsonValue::Parse(strData); JsonArray^ myArray = jsonValue->GetArray();
//请求的数据是对的,可能要改一下方法,因为绑定CollectionViewSource ,我不知道怎么办。 for(int i=0;i<10;i++) { DataBox^ databox = ref new DataBox(); databox->catename = myArray->GetObjectAt(i)->GetNamedString("catename");//请求10个分类 战争/科幻/...等 JsonArray^ moviesArray = myArray->GetObjectAt(i)->GetNamedArray("movies"); //每个分类下面放6个电影(img,和 title) for(int j=0;j<6;j++) { HomeData^ homeData = ref new HomeData(); homeData->title = moviesArray->GetObjectAt(j)->GetNamedString("title");//图片地址 homeData->imgurl= moviesArray->GetObjectAt(j)->GetNamedString("img");// 影片名称 dataBox->ite->Append(homeData); } this->DataContext = dataBox; }
3,xaml绑定分组
<CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding ite}" //MSDN上面说这里要绑定大数据源,比如databox ,这里我不知道怎么写,求助:不知道绑那一个,请大神看一下代码。 IsSourceGrouped="true" ItemsPath="ite"/>//MSDN上面说这里是绑定小类,比如 databox->ite 下面的图片地址 imgurl,标题title
<GridView x:Name="itemGridView" AutomationProperties.AutomationId="ItemGridView" AutomationProperties.Name="Grouped Items" Grid.Row="1" Margin="0,-3,0,0" Padding="116,0,40,46" ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}" //MSDN上说,这里绑定 上面定义的collectionviewsource SelectionMode="None" IsHoldingEnabled="True" ItemTemplate="{StaticResource Custom153x204ItemTemplate}" ItemsPanel="{StaticResource StoreFrontGridItemsPanelTemplate}">
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Content="{bingding path= catename}" //绑定分类
Style="{StaticResource TextButtonStyle}"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>求助:
<CollectionViewSource
x:Name="groupedItemsViewSource"
Source="{Binding ite}" //这里我不知道怎么写。
IsSourceGrouped="true"
ItemsPath="ite"/> //绑定小类 ,我写的ite,不知道对否,求大神帮助。
- 已编辑 英明神武可爱 2012年8月31日 2:22
答案
-
CollectionViewSource 的Source需要绑定到一个数据集合上,所以你的this->DataContext 已经是 databox了,所以这里Source 是对的,绑定到databox的ite属性,即Vector<HomeData^>上。
但是小类写错了,ite下面的每个元素没有ite属性,所以这里小类是找不到的。你这里没有分组,所以不用写小类 ItemsPath。
况且你的数据元素 HomeData 也没有分组,所以你也不需要 IsSourceGrouped="true",且不需要设置GridView.GroupStyle。
---------------------
如果你要分组,你需要下面这样的结构。
需要增加一个Group 类型,且将你的HomeData放入这个Group中的一个集合属性,然后你照旧绑定CollectionViewSource Source为ite,然后ItemsPath指定为items (这个items就是ite集合中每个元素 HomeDataGroup 的属性 items
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 英明神武可爱 2012年9月4日 13:37