Tag Archives: ListView Controls

Windows 8: Adding Items from a File System

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-expertkgetzT
his 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.

Windows 8 Using XAML: Choosing a ListView or GridView Control

Windows 8 ListView and GridView controls both allow you to display data, but they display their collections of data differently. The following article takes a look at both controls and how they are used.

Choosing a ListView or GridView

In order to display multiple data items, you can select the ListView or GridView control. Both controls display collections of data, making it easy to present items from a list, or any other bound data source. Both controls provide similar functionality, although they display their data slightly differently. Both controls derive from the base ItemsControl parent class, and both inherit the functionality of the base control. Because both controls inherit from the same base class, they share a large number of properties. As a matter of fact, the controls are so similar, they’re often both referred to using the name ItemsControl (the base class), rather than saying “ListView and GridView controls”.

TIP: To be honest, the ListView and GridView controls are so similar, you can think of them as a single control. The major difference between the two is the manner in which they lay out their items, and it’s easy to change the layout so each control emulates the other.

The ListView control displays its data stacked vertically, much like a standard list box. Use this control to display an ordered list of data, like a list of email messages, a list of contacts, or search results. It’s also useful in master-detail layout scenarios—you can display the list of master items in a ListView control, and the details in some other kind of control. You can control every aspect of the layout of data in this control, including how each data item appears in the list, using templates.

The GridView control displays its data stacked horizontally in rows (although you can alter this behavior and have it displayed in columns first, as well). You use this control when you need more space for rich visualization of each item to be displayed. As with the ListView control, you can use templates to control the layout of each item, displaying the data in any manner you need.

There are several ways to display data in an ItemsControl. You can:

  • Add items directly to the Items collection property, in XAML or in code.
  • Bind the ItemsSource property to a data source.

Applications often use both a ListView and a GridView control bound to the same set of data, so you can alter the view based on the current screen layout. For example, you might use a GridView control in full-screen layout, but a ListView when in Snapped layout. The first Figure below shows a sample application using the GridView control, and the second Figure below shows the same application, in Snapped view. The trick here is to show and hide the two controls, based on the current layout.

using a GridView control

using a ListView control


Thumbnail for 559T
his 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.