--- title: "ListView Overview" description: Learn about the Windows Presentation Foundation ListView control, which provides the infrastructure to display data items in different layouts or views. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "controls [WPF], ListView" - "ListView controls [WPF], about ListView control" ms.assetid: 989e12b0-260e-4570-95c6-489284003ce2 --- # ListView Overview The control provides the infrastructure to display a set of data items in different layouts or views. For example, a user may want to display data items in a table and also to sort its columns. > [!NOTE] > The types referenced in this article are available in the [Code reference](#code-reference) section. ## What Is a ListView? The control is an that is derived from . Typically, its items are members of a data collection and are represented as objects. A is a and can contain only a single child element. However, that child element can be any visual element. ## Defining a View Mode for a ListView To specify a view mode for the content of a control, you set the property. One view mode that Windows Presentation Foundation (WPF) provides is , which displays a collection of data items in a table that has customizable columns. The following example shows how to define a for a control that displays employee information. [!code-xaml[ListViewCode#ListViewEmployee](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml#listviewemployee)] The following illustration shows how the data appears for the previous example. ![Screenshot that shows a ListView with GridView output.](./media/gridview-overview/listview-gridview-output.jpg) You can create a custom view mode by defining a class that inherits from the class. The class provides the infrastructure that you need to create a custom view. For more information about how to create a custom view, see [Create a Custom View Mode for a ListView](how-to-create-a-custom-view-mode-for-a-listview.md). ## Binding Data to a ListView Use the and properties to specify items for a control. The following example sets the property to a data collection that is called `EmployeeInfoDataSource`. [!code-xaml[ListViewCode#ItemsSource](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml#itemssource)] In a , objects bind to specified data fields. The following example binds a object to a data field by specifying a for the property. [!code-csharp[ListViewCode#GridViewColumnProperties](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml.cs#gridviewcolumnproperties)] [!code-vb[ListViewCode#GridViewColumnProperties](~/samples/snippets/visualbasic/VS_Snippets_Wpf/ListViewCode/visualbasic/window1.xaml.vb#gridviewcolumnproperties)] [!code-xaml[ListViewCode#GridViewColumnProperties](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml#gridviewcolumnproperties)] You can also specify a as part of a definition that you use to style the cells in a column. In the following example, the that is identified with a sets the for a . Note that this example does not define the because doing so overrides the binding that is specified by . [!code-xaml[ListViewTemplate#GridViewCellTemplate](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewTemplate/CS/window1.xaml#gridviewcelltemplate)] [!code-xaml[ListViewTemplate#CellTemplateProperty](~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewTemplate/CS/window1.xaml#celltemplateproperty)] ## Styling a ListView That Implements a GridView The control contains objects, which represent the data items that are displayed. You can use the following properties to define the content and style of data items: - On the control, use the , , and properties. - On the control, use the and properties. To avoid alignment issues between cells in a , do not use the to set properties or add content that affects the width of an item in a . For example, an alignment issue can occur when you set the property in the . To specify properties or define content that affects the width of items in a , use the properties of the class and its related classes, such as . For more information about how to use and its supporting classes, see [GridView Overview](gridview-overview.md). If you define an for a control and also define an , you must include a in the style in order for the to work correctly. Do not use the and properties for content that is displayed by using a . To specify the alignment of content in a column of a , define a . ## Sharing the Same View Mode Two controls cannot share the same view mode at the same time. If you try to use the same view mode with more than one control, an exception occurs. To specify a view mode that can be simultaneously used by more than one , use templates or styles. ## Creating a Custom View Mode Customized views like are derived from the abstract class, which provides the tools to display data items that are represented as objects. ## Code reference The following objects are referenced in this article: - `EmployeeInfoDataSource` data collection. If you're using Visual Basic .NET, the `Window` element is declared slightly different from what you see in the example code: :::code language="xaml" source="~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml" range="1-8" highlight="4,6-8" ::: - `EmployeeInfo` class, which is used as the type for the `EmployeeInfoDataSource` data collection. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_Wpf/ListViewCode/CSharp/Window1.xaml.cs" range="66-96" ::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_Wpf/ListViewCode/visualbasic/window1.xaml.vb" range="63-100" ::: ## See also - - - - - [GridView Overview](gridview-overview.md) - [How-to Topics](listview-how-to-topics.md) - [Controls](../advanced/optimizing-performance-controls.md)