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:
- 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.
- Retrieve a reference to a specific folder, or use a known folder (an item in the KnownFolders class).
- Create an instance of the FileQuery class, calling the CreateFileQuery method of a storage folder, optionally supplying the QueryOptions object you created previously.
- Create an instance of the FileInformationFactory class, passing the FileQuery you created previously, and optionally, other parameters.
- Set the ItemsSource property of an ItemsControl to the results of calling one of the FileInformationFactory’s methods, such as GetVirtualizedFilesVector.
This 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 is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.