第一组标题在Listview中无法正确显示

时间:2014-08-26 00:11:09

标签: c# xaml windows-phone

我正在使用XAML和C#中的Windows Phone 8.1应用程序,当数据加载时,列表视图不会显示第一组项目的组头。

这是我的代码:

Player.xaml

<Page
    x:Class="TestApp.Player"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <DataTemplate x:Key="GroupTemplate">
            <Grid Grid.Column="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="1">
                    <TextBlock x:Name="SongTitle" Text="{Binding Title}"
                               Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                    <TextBlock x:Name="ArtistName" Text="{Binding Album}"
                               Style="{ThemeResource ListViewItemContentTextBlockStyle}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <CollectionViewSource x:Name="MusicSource" IsSourceGrouped="true" />
    </Page.Resources>
    <Grid>
        <ListView
            x:Name="contentList"
            SelectionMode="Multiple"
            ItemTemplate="{StaticResource GroupTemplate}"
            ItemsSource="{Binding Source={StaticResource MusicSource}}">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel HorizontalAlignment="Stretch" Background="#FFB83536" Width="{Binding ActualWidth, ElementName=contentList}">
                                <TextBlock Text="{Binding Key}" />
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>
    </Grid>
</Page>

Player.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using TestApp;
using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage.Search;
using Windows.Storage.FileProperties;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace TestApp
{
    public sealed partial class Player : Page
    {
        ObservableCollection<Music> source = new ObservableCollection<Music>();
        public Player()
        {
            this.InitializeComponent();
            this.SetItemSource();
        }

        private async Task getMusic(IStorageFolder folder)
        {
            var folders = await folder.GetFoldersAsync();
            if (folders != null)
                foreach (var fol in folders)
                    await getMusic(fol);

            var files = await folder.GetFilesAsync();
            foreach (var file in files)
            {
                MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
                this.source.Add(new Music(musicProperties.Artist, musicProperties.Title, musicProperties.Album));
                var result = from act in this.source group act by act.Album into grp orderby grp.Key select grp;
                MusicSource.Source = result;
            }
        }

        public async void SetItemSource()
        {
            await getMusic(Windows.Storage.KnownFolders.MusicLibrary);
            this.UpdateLayout();
        }
    }
}

这只是第一组的标题仍然完全空白。第二组和之后的任何内容都能正确显示组头。

第一个标题只是空白空格,当您向下滚动列表时,您可以在项目顶部看到它。

0 个答案:

没有答案