9.4 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|---|
| ListView Overview | Learn about the Windows Presentation Foundation ListView control, which provides the infrastructure to display data items in different layouts or views. | 03/30/2017 |
|
|
989e12b0-260e-4570-95c6-489284003ce2 |
ListView Overview
The xref:System.Windows.Controls.ListView 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 section.
What Is a ListView?
The xref:System.Windows.Controls.ListView control is an xref:System.Windows.Controls.ItemsControl that is derived from xref:System.Windows.Controls.ListBox. Typically, its items are members of a data collection and are represented as xref:System.Windows.Controls.ListViewItem objects. A xref:System.Windows.Controls.ListViewItem is a xref:System.Windows.Controls.ContentControl 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 xref:System.Windows.Controls.ListView control, you set the xref:System.Windows.Controls.ListView.View%2A property. One view mode that [!INCLUDETLA#tla_winclient] provides is xref:System.Windows.Controls.GridView, which displays a collection of data items in a table that has customizable columns.
The following example shows how to define a xref:System.Windows.Controls.GridView for a xref:System.Windows.Controls.ListView control that displays employee information.
[!code-xamlListViewCode#ListViewEmployee]
The following illustration shows how the data appears for the previous example.
You can create a custom view mode by defining a class that inherits from the xref:System.Windows.Controls.ViewBase class. The xref:System.Windows.Controls.ViewBase 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.
Binding Data to a ListView
Use the xref:System.Windows.Controls.ItemsControl.Items%2A and xref:System.Windows.Controls.ItemsControl.ItemsSource%2A properties to specify items for a xref:System.Windows.Controls.ListView control. The following example sets the xref:System.Windows.Controls.ItemsControl.ItemsSource%2A property to a data collection that is called EmployeeInfoDataSource.
[!code-xamlListViewCode#ItemsSource]
In a xref:System.Windows.Controls.GridView, xref:System.Windows.Controls.GridViewColumn objects bind to specified data fields. The following example binds a xref:System.Windows.Controls.GridViewColumn object to a data field by specifying a xref:System.Windows.Data.Binding for the xref:System.Windows.Controls.GridViewColumn.DisplayMemberBinding%2A property.
[!code-csharpListViewCode#GridViewColumnProperties] [!code-vbListViewCode#GridViewColumnProperties] [!code-xamlListViewCode#GridViewColumnProperties]
You can also specify a xref:System.Windows.Data.Binding as part of a xref:System.Windows.DataTemplate definition that you use to style the cells in a column. In the following example, the xref:System.Windows.DataTemplate that is identified with a xref:System.Windows.ResourceKey sets the xref:System.Windows.Data.Binding for a xref:System.Windows.Controls.GridViewColumn. Note that this example does not define the xref:System.Windows.Controls.GridViewColumn.DisplayMemberBinding%2A because doing so overrides the binding that is specified by xref:System.Windows.DataTemplate.
[!code-xamlListViewTemplate#GridViewCellTemplate]
[!code-xamlListViewTemplate#CellTemplateProperty]
Styling a ListView That Implements a GridView
The xref:System.Windows.Controls.ListView control contains xref:System.Windows.Controls.ListViewItem 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 xref:System.Windows.Controls.ListView control, use the xref:System.Windows.Controls.ItemsControl.ItemTemplate%2A, xref:System.Windows.Controls.ItemsControl.ItemTemplateSelector%2A, and xref:System.Windows.Controls.ItemsControl.ItemContainerStyle%2A properties.
-
On the xref:System.Windows.Controls.ListViewItem control, use the xref:System.Windows.Controls.ContentControl.ContentTemplate%2A and xref:System.Windows.Controls.ContentControl.ContentTemplateSelector%2A properties.
To avoid alignment issues between cells in a xref:System.Windows.Controls.GridView, do not use the xref:System.Windows.Controls.ItemsControl.ItemContainerStyle%2A to set properties or add content that affects the width of an item in a xref:System.Windows.Controls.ListView. For example, an alignment issue can occur when you set the xref:System.Windows.FrameworkElement.Margin%2A property in the xref:System.Windows.Controls.ItemsControl.ItemContainerStyle%2A. To specify properties or define content that affects the width of items in a xref:System.Windows.Controls.GridView, use the properties of the xref:System.Windows.Controls.GridView class and its related classes, such as xref:System.Windows.Controls.GridViewColumn.
For more information about how to use xref:System.Windows.Controls.GridView and its supporting classes, see GridView Overview.
If you define an xref:System.Windows.Controls.ItemsControl.ItemContainerStyle%2A for a xref:System.Windows.Controls.ListView control and also define an xref:System.Windows.Controls.ItemsControl.ItemTemplate%2A, you must include a xref:System.Windows.Controls.ContentPresenter in the style in order for the xref:System.Windows.Controls.ItemsControl.ItemTemplate%2A to work correctly.
Do not use the xref:System.Windows.Controls.Control.HorizontalContentAlignment%2A and xref:System.Windows.Controls.Control.VerticalContentAlignment%2A properties for xref:System.Windows.Controls.ListView content that is displayed by using a xref:System.Windows.Controls.GridView. To specify the alignment of content in a column of a xref:System.Windows.Controls.GridView, define a xref:System.Windows.Controls.GridViewColumn.CellTemplate%2A.
Sharing the Same View Mode
Two xref:System.Windows.Controls.ListView controls cannot share the same view mode at the same time. If you try to use the same view mode with more than one xref:System.Windows.Controls.ListView control, an exception occurs.
To specify a view mode that can be simultaneously used by more than one xref:System.Windows.Controls.ListView, use templates or styles.
Creating a Custom View Mode
Customized views like xref:System.Windows.Controls.GridView are derived from the xref:System.Windows.Controls.ViewBase abstract class, which provides the tools to display data items that are represented as xref:System.Windows.Controls.ListViewItem objects.
Code reference
The following objects are referenced in this article:
-
EmployeeInfoDataSourcedata collection. If you're using Visual Basic .NET, theWindowelement 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" :::
-
EmployeeInfoclass, which is used as the type for theEmployeeInfoDataSourcedata 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" :::
