Export (0) Print
Expand All

ListView Class

Updated: February 2010

Represents a control that displays a list of data items.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(ListViewItem))]
public class ListView : ListBox
<ListView>
  Items
</ListView>

Content Model: ListView is an ItemsControl. Its content properties are Items and ItemsSource. For more information on the content model for ListView, see Controls Content Model Overview.

The presentation of the data items in a ListView is defined by its view mode, which is specified by the View property. Windows Presentation Foundation (WPF) provides a GridView view mode that partitions the ListView data item content into columns. The properties and methods on GridView and its related classes style and specify the content of the columns.

The following illustration shows a ListView with a GridView view.

ListView with GridView output

You can also define custom views by creating a class that derives from ViewBase. For more information, see How to: Create a Custom View Mode for a ListView.

If you define a custom ItemContainerStyle for a ListView control and also define an ItemTemplate, you must include a ContentPresenter in the ItemContainerStyle. The ContentPresenter is required for the ItemTemplate to work correctly.

To avoid alignment issues in a GridView view mode, do not add content or set properties that affect the width of an item in the ItemContainerStyle of a ListView. To prevent alignment issues, use properties and templates that are defined for the GridView view mode. This includes properties and templates that are defined for classes that are used in a GridView view mode, such as GridViewColumn and GridViewColumnHeader.

Two ListView controls cannot share the same view. To specify a view that can be used more than one time, use templates or styles. For an example of a GridView and other custom views that are defined as reusable resources, see ListView with Multiple Views Sample.

Do not use the HorizontalContentAlignment and VerticalContentAlignment properties for ListView content that is displayed by using a GridView. To specify the alignment of content in a column of a GridView, define a CellTemplate.

Displaying a large number of items may cause performance issues. See Optimizing Performance: Controls for more information.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

The following example shows how to create a ListView control that implements a GridView as its View. For the complete sample, see ListView That Uses a GridView Sample. The illustration in the Remarks section shows how this example looks.

<ListView ItemsSource="{Binding Source=
                       {StaticResource EmployeeInfoDataSource}}">

  <ListView.View>

    <GridView AllowsColumnReorder="true"
              ColumnHeaderToolTip="Employee Information">

      <GridViewColumn DisplayMemberBinding=
                          "{Binding Path=FirstName}" 
                      Header="First Name" Width="100"/>

                  <GridViewColumn DisplayMemberBinding=
                          "{Binding Path=LastName}" 
                      Width="100">
                      <GridViewColumnHeader>Last Name
                          <GridViewColumnHeader.ContextMenu>
                          <ContextMenu  MenuItem.Click="LastNameCM_Click"  
                                        Name="LastNameCM">
                              <MenuItem Header="Ascending" />
                              <MenuItem Header="Descending" />
                          </ContextMenu>
                          </GridViewColumnHeader.ContextMenu>
                      </GridViewColumnHeader>
                  </GridViewColumn>

                  <GridViewColumn DisplayMemberBinding=
                          "{Binding Path=EmployeeNumber}" 
                      Header="Employee No." Width="100"/>
    </GridView>

  </ListView.View>
</ListView>

More Code

How to: Display ListView Contents by Using a GridView This example shows how to define a GridView view mode for a ListView control.
How to: Use Templates to Style a ListView That Uses GridView This example shows how to use the DataTemplate and Style objects to specify the appearance of a ListView control that uses a GridView view mode.
How to: Sort a GridView Column When a Header Is Clicked This example shows how to create a ListView control that implements a GridView view mode and sorts the data content when a user clicks a column header.
How to: Create a Custom View Mode for a ListView This example shows how to create a custom View mode for a ListView control.
How to: Create a ListView with Editable Cells This example shows how to create a ListView control in a GridView view mode that has editable cells.
How to: Use Triggers to Style Selected Items in a ListView This example shows how to define Triggers for a ListViewItem control so that when a property value of a ListViewItem changes, the Style of the ListViewItem changes in response.
How to: Alternate the Background Color for Rows in a ListView This example shows three methods that you can use to alternate the Background color for rows in a ListView.
How to: Create ListViewItems with a CheckBox This example shows how to display a column of CheckBox controls in a ListView control that uses a GridView.
How to: Create a Style for a Dragged GridView Column Header This example shows how to change the appearance of a dragged GridViewColumnHeader when the user changes the position of a column.
How to: Style a Row in a ListView That Implements a GridView This example shows how to style a row in a ListView control that implements a GridView View mode.
How to: Group Items in a ListView That Implements a GridView This example shows how to display groups of items in the GridView view mode of a ListView control.
How to: Display Data by Using GridViewRowPresenter This example shows how to use the GridViewRowPresenter and GridViewHeaderRowPresenter objects to display data in columns.

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Media.Visual
        System.Windows.UIElement
          System.Windows.FrameworkElement
            System.Windows.Controls.Control
              System.Windows.Controls.ItemsControl
                System.Windows.Controls.Primitives.Selector
                  System.Windows.Controls.ListBox
                    System.Windows.Controls.ListView

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

February 2010

Explained illustration on page was from example code.

Customer feedback.

February 2009

Described how default styles change dependency properties.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft