我想要使用XAML绑定到GridView的视频列表。我使用以下代码以编程方式执行了该操作:
private async void pageRoot_Loaded(object sender, RoutedEventArgs e) {
IList<string> fileTypeFilter = new List<string> { ".avi", ".mkv", ".mp4" };
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);
queryOptions.FolderDepth = FolderDepth.Deep;
queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
StorageFileQueryResult videoFileQuery = KnownFolders.VideosLibrary.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> dataSource = await videoFileQuery.GetFilesAsync();
videoGridView.ItemsSource = dataSource; //Yep, that works :)
}
但是,我想在XAML中拥有这个绑定。所以我更改了XAML GridView并删除了上述代码的最后一行:
<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding dataSource}"/>
现在只有GridView为空。
我该怎么做才能实现这个目标?
答案 0 :(得分:1)
只需进行2次更改。首先,代替发布代码的最后一行,写下以下
this.DataContext = dataSource;
然后将Grid
更改为此
<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding}"/>
答案 1 :(得分:0)
尝试绑定到ObservableCollection类型的源&lt; StorageFile&gt;