绑定列表视图组头

use*_*509 3 c# data-binding xaml listview windows-10

因此,我尝试非常简单地在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>
Run Code Online (Sandbox Code Playgroud)

以及由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;
Run Code Online (Sandbox Code Playgroud)

(EPISODELST和情节是两个类,但没有必要在此处粘贴它们)

我已经在线查看了分组列表视图的各种其他实现,但是它们都比这复杂得多,而且我猜想这应该可行,因为我可以告诉代码可以成功地对所有数据进行正确排序。问题可能与TextBlock的绑定有关,但是我尝试了其他在线发现的其他事情,例如{Binding = Name}或{Binding Key.Name},但似乎没有任何效果。

use*_*509 5

因此,最后,这确实很简单。我发现遮篷掩盖了Microsoft的UWP Github示例页面的深处。它必须绑定到{Binding Key}

内部GroupStyle:

<TextBlock Text="{Binding Key}"/>
Run Code Online (Sandbox Code Playgroud)