Blog

Windows 8: Adding Items from a File System

Excerpt by Ken Getz

windowslivewriter_customplacementwithinanitemscontrolinwpf_ee53_image_6 In order to take full advantage of an ItemsControl, you must be able to bind it to an existing collection of data. Most of the time, you won't add items to an ItemsControl individually. Instead, you'll most likely bind the ItemsControl to some sort of data source. Any data that can be stored and retrieved as a list or array of items is fair game for display in an ItemsControl.

Data in an ItemsControl can come from any collection of data, such as an inmemory data structure, the results of calling a Web service, or data retrieved from a database. It's really simple to bind an ItemsControl to any collection of data in memory: You set the ItemsSource property to the collection of data, and the ItemsControl does the rest of the work.

Adding Items from File System

What if you wanted to bind ItemsControl to some real data? The simplest place to get "real" data is from the file system, and you can easily bind an ItemsControl to information from the file system, with a little effort.

Although it's not difficult to bind to information from the file system, it does require a little more effort than you might expect. In order to retrieve any information, you'll need to create an instance of the FileInformationFactory class, which provides several use methods:

  • GetFilesAsync: Retrieves a collection of FileInformation objects. The method does its work asynchronously, but returns the full file list.
  • GetVirtualizedFilesVector: Retrieves a virtualized vector of IStorageInformation objects. By virtualizing its results, this method makes it possible for an ItemsControl to effectively display a huge amount of data. Scrolling through the control causes the data source to retrieve more data.
  • GetVirtualizedFoldersVector: Works like the GetVirtualizedFilesVector method, but retrieves only folderinformation.
  • GetVirtualizedItemsVector: Works like the GetVirtualizedFilesVector method, but retrieves both file and folder information.

NOTE: The IStorageInformation class acts as a base class for information about files and folders.

In order to bind to data retrieved from the file system, you will also need to follow these steps:

  1. Optionally, create a QueryOptions object whose properties describe the requested sort order and filtering. If you don't specify a QueryOptions instance, you will retrieve data in natural order including all the filesand/or folders.
  2. Retrieve a reference to a specific folder, or use a known folder (an item in the KnownFolders class).
  3. Create an instance of the FileQuery class, calling the CreateFileQuery method of a storage folder, optionally supplying the QueryOptions object you created previously.
  4. Create an instance of the FileInformationFactory class, passing the FileQuery you created previously, and optionally, other parameters.
  5. Set the ItemsSource property of an ItemsControl to the results of calling one of the FileInformationFactory's methods, such as GetVirtualizedFilesVector.

ldn-expertkgetzThis post is an excerpt from the online courseware for our Windows 8 Using XAML: Views, Resources, and Toasts course written by expert Ken Getz.



Ken Getz
Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.


This course excerpt was originally posted June 18, 2013 from the online courseware Windows 8 Using XAML, Part 09: Views, Binding, and Templates by Ken Getz

Windows 8 XAML