绑定列表视图组 header
Binding listview group header
因此,我试图非常简单地在 Windows 10 列表视图中显示项目,然后按组将它们分开。一切正常,除了我似乎无法绑定组的标题。
这是我现在的 xaml:
<ListView ItemsSource="{Binding Source={StaticResource cvsEpisodes}}"/>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding EpisodeNB}"/>
<TextBlock Text="{Binding EpisodeTT}"/>
<TextBlock Text="{Binding EpisodeDESC}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding SEASONNB}"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<Page.Ressources>
<CollectionViewSource x:Name="cvsEpisodes" IsSourceGrouped="True"/>
</Page.Ressources>
以及由 OnNavigatedTo 事件执行的 C#:
List < EPISODELST > Episodes = new List < EPISODELST > ();
var episodes = root.episodes.Select(m = >new EPISODELST {EpisodeTT = m.title, EpisodeNB = m.episode.ToString(), EpisodeDESC = m.overview, SEASONNB = m.season.ToString()}).ToList();
foreach(EPISODELST s in episodes)
{
Episodes.Add(new EPISODELST {EpisodeTT = s.EpisodeTT, EpisodeDESC = s.EpisodeDESC, EpisodeNB = "EPISODE " + s.EpisodeNB, SEASONNB = s.SEASONNB });
}
var result = from EPISODELST in Episodes group EPISODELST by EPISODELST.SEASONNB into grp orderby grp.Key select grp;
cvsEpisodes.Source = result;
(EPISODELST和剧集是两个类,但没有必要在这里粘贴它们)
我在网上看到过分组列表视图的各种其他实现,但它们都比这复杂得多,我猜这应该可行,因为我可以告诉代码可以成功地正确排序所有数据。
问题可能只与 TextBlock 的绑定有关,但我尝试了在网上找到的各种其他方法,例如 {Binding=Name} 或 {Binding Key.Name},但似乎没有任何效果。
所以,最后,这真的很简单。我找到了深埋的答案 Microsoft's UWP Github sample page。
它必须绑定到 {Binding Key}
GroupStyle 内部:
<TextBlock Text="{Binding Key}"/>
因此,我试图非常简单地在 Windows 10 列表视图中显示项目,然后按组将它们分开。一切正常,除了我似乎无法绑定组的标题。
这是我现在的 xaml:
<ListView ItemsSource="{Binding Source={StaticResource cvsEpisodes}}"/>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding EpisodeNB}"/>
<TextBlock Text="{Binding EpisodeTT}"/>
<TextBlock Text="{Binding EpisodeDESC}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding SEASONNB}"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<Page.Ressources>
<CollectionViewSource x:Name="cvsEpisodes" IsSourceGrouped="True"/>
</Page.Ressources>
以及由 OnNavigatedTo 事件执行的 C#:
List < EPISODELST > Episodes = new List < EPISODELST > ();
var episodes = root.episodes.Select(m = >new EPISODELST {EpisodeTT = m.title, EpisodeNB = m.episode.ToString(), EpisodeDESC = m.overview, SEASONNB = m.season.ToString()}).ToList();
foreach(EPISODELST s in episodes)
{
Episodes.Add(new EPISODELST {EpisodeTT = s.EpisodeTT, EpisodeDESC = s.EpisodeDESC, EpisodeNB = "EPISODE " + s.EpisodeNB, SEASONNB = s.SEASONNB });
}
var result = from EPISODELST in Episodes group EPISODELST by EPISODELST.SEASONNB into grp orderby grp.Key select grp;
cvsEpisodes.Source = result;
(EPISODELST和剧集是两个类,但没有必要在这里粘贴它们)
我在网上看到过分组列表视图的各种其他实现,但它们都比这复杂得多,我猜这应该可行,因为我可以告诉代码可以成功地正确排序所有数据。 问题可能只与 TextBlock 的绑定有关,但我尝试了在网上找到的各种其他方法,例如 {Binding=Name} 或 {Binding Key.Name},但似乎没有任何效果。
所以,最后,这真的很简单。我找到了深埋的答案 Microsoft's UWP Github sample page。
它必须绑定到 {Binding Key}
GroupStyle 内部:
<TextBlock Text="{Binding Key}"/>