简体   繁体   English

如何将GridView绑定到XAML中的视频列表?

[英]How to bind a GridView to a list of videos in XAML?

I have a list of videos I want to bind to a GridView using XAML. 我有一个要使用XAML绑定到GridView的视频列表。 I did that programmatically with the following code and it is working: 我使用以下代码以编程方式进行了此操作,并且可以正常工作:

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 :)
}

Howevery, I'd like to have this binding in XAML. 但是,我想在XAML中具有此绑定。 So I changed the XAML GridView and removed the last line of the aforementioned code: 因此,我更改了XAML GridView并删除了上述代码的最后一行:

<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding dataSource}"/>

Only that now the GridView is empty. 只是现在GridView是空的。

What must I do to achieve this? 我必须怎么做才能做到这一点?

Just make 2 changes. 只需进行2个更改。 First, instead of last line of your posted code, write following 首先,而不是您发布的代码的最后一行,编写以下内容

this.DataContext = dataSource;

Then change your Grid to this 然后将您的Grid更改为此

<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding}"/>

尝试绑定到ObservableCollection <StorageFile>类型的源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM