Click here to Skip to main content
6,305,776 members and growing! (17,616 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » DataGrid and DataView     Intermediate License: The Code Project Open License (CPOL)

ListView Layout Manager

By Jani Giannoudis

WPF: Customizing ListView/GridView Column-Layout
C# 3.0, Windows, .NET 3.0, XAML, WPF, Dev
Posted:8 Apr 2008
Updated:28 Nov 2008
Views:66,019
Bookmarked:114 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
30 votes for this article.
Popularity: 6.89 Rating: 4.66 out of 5

1
2 votes, 9.1%
2

3
4 votes, 18.2%
4
16 votes, 72.7%
5
ListViewLayoutManager.gif

Introduction

Using a ListViewLayoutManager allows controlling the behavior of the column layout of ListView/GridView controls:

  • Fixed Column: Column with fixed column width
  • Range Column: Column with minimal and/or maximal column width
  • Proportional Column: Column with proportional column width

The Range Column allows to restrict the column width as well as to fill the remaining visible area with the column.

As known from HTML tables or the Grid control, the Proportional Column determines the column widths on a percentage basis. The following factors determine the width of a proportional column:

  • Visibility of the vertical ListView scrollbars
  • Changes of the ListView control width
  • Changes of the width of a non-proportional column

The implementation supports both controlling through XAML or Code Behind. Usage of XAML styles allows a ListViewLayoutManager to be 'attached' to an existing ListView control.

The class ConverterGridColumn offers object specific binding by using the interface IValueConverter. Using the ImageGridViewColumn class allows representing a column as an image/icon using a DataTemplate.

In the article User Settings Applied, I describe how you can persist order and size of the ListView columns.

ListView/GridView Layout in XAML

Fixed Column

The following example shows controlling columns with fixed widths using XAML:

  <ListView
    Name="MyListView"
    ctrl:ListViewLayoutManager.Enabled="true">

    <ListView.View>
      <GridView>
        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Name}"

          ctrl:FixedColumn.Width="100"
          Header="Name" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=City}"
          ctrl:FixedColumn.Width="300"

          Header="City" />
      </GridView>

    </ListView.View>
  </ListView>

Setting the property Enabled binds the ListViewLayoutManager to the ListView control. The property FixedColumn.Width determines the column width and prevents resizing using the mouse.

Proportional Column

The following example shows controlling columns with proportional widths using XAML:

  <ListView
    Name="MyListView"
    ctrl:ListViewLayoutManager.Enabled="true">
    <ListView.View>

      <GridView>

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Name}"
          ctrl:ProportionalColumn.Width="1"

          Header="Name" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=City}"

          ctrl:ProportionalColumn.Width="3"
          Header="City" />
      </GridView>

    </ListView.View>

  </ListView>

Matching the RowDefinition.Width of the Grid control, the value of ProportionalColumn.Width represents the percentage. The scenario above sets the column Name to 25% and the column City to 75% of the total width. Analogous to the fixed columns, resizing with the mouse is disabled.

Range Column

The following example shows controlling ranged columns with minimal/maximal widths using XAML:

  <ListView
    Name="MyListView"
    ctrl:ListViewLayoutManager.Enabled="true">
    <ListView.View>

      <GridView>

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Name}"
          ctrl:RangeColumn.MinWidth="100"
          Width="150"

          Header="Name" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=City}"
          ctrl:RangeColumn.MaxWidth="200"
          Width="150"

          Header="City" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Country}"
          Width="100"
          ctrl:RangeColumn.MinWidth="50"

          ctrl:RangeColumn.MaxWidth="150"
          Header="Country" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=State}"
          Width="100"

          ctrl:RangeColumn.MinWidth="100"
          ctrl:RangeColumn.IsFillColumn="true"
          Header="Country" />

      </GridView>

    </ListView.View>
  </ListView>

The first range column which has the value IsFillColumn set to true, will automatically get resized to the remaining space. The column will not get filled if the ListView contains a proportional column.

Dragging the mouse out of the configured range when resizing, the mouse cursor changes its representation to indicate this.

Combined Usage

In real life, it is common to combine these column types. Their order can be varied as required:

  <ListView
    Name="MyListView"
    ctrl:ListViewLayoutManager.Enabled="true">

    <ListView.View>
      <GridView>

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=State}"
          ctrl:FixedColumn.Width="25"
          Header="Name" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Name}"

          Width="150"
          ctrl:RangeColumn.MinWidth="100"
          ctrl:RangeColumn.MaxWidth="200"

          Header="City" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=City}"
          ctrl:ProportionalColumn.Width="1"
          Header="Zip" />

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Country}"

          ctrl:ProportionalColumn.Width="2"
          Header="Country" />
      </GridView>

    </ListView.View>
  </ListView>

ListView/GridView Layout using Code Behind

It is also possible to setup the column layout in the source code:

 ListView listView = new ListView();
 new ListViewLayoutManager( listView ); // attach the layout manager

 GridView gridView = new GridView();
 gridView.Columns.Add( FixedColumn.ApplyWidth( new MyGridViewColumn( "State" ), 25 ) );
 gridView.Columns.Add( RangeColumn.ApplyWidth( new MyGridViewColumn( "Name" ), 100,
     150, 200 ) ); // 100...200
 gridView.Columns.Add( ProportionalColumn.ApplyWidth( new MyGridViewColumn( "City" ),
     1 ) ); // 33%
 gridView.Columns.Add( ProportionalColumn.ApplyWidth( new MyGridViewColumn(
     "Country" ), 2 ) ); // 66%

 listView.View = gridView;

Columns with Custom Representation

The class ConverterGridColumn serves as a base class for binding table columns to individual objects:

  // ------------------------------------------------------------------------
  public class Customer
  {
    // ----------------------------------------------------------------------
    public Customer()
    {
    } // Customer

    // ----------------------------------------------------------------------
    public string FirstName
    {
      get { return this.firstName; }
      set { this.firstName = value; }
    } // FirstName

    // ----------------------------------------------------------------------
    public string LastName
    {
      get { return this.lastName; }
      set { this.lastName = value; }
    } // LastName

    // ----------------------------------------------------------------------
    // members
    private string firstName;
    private string lastName;

  } // class Customer

  // ------------------------------------------------------------------------
  public class CustomerFullNameColumn : ConverterGridViewColumn
  {
    // ----------------------------------------------------------------------
    public CustomerGridViewColumn() :
      base( typeof( Customer ) )
    {
    } // CustomerGridViewColumn

    // ----------------------------------------------------------------------
    protected override object ConvertValue( object value )
    {
      Customer customer = value as Customer;
      return string.Concat( customer.FirstName, " ", customer.LastName );
    } // ConvertValue

  } // class CustomerFullNameColumn

Columns Represented as Images

The class ImageGridColumn serves as a base class for binding table columns to images/icons:

  // ------------------------------------------------------------------------
  public class Customer
  {
    // ----------------------------------------------------------------------
    public Customer()
    {
    } // Customer

    // ----------------------------------------------------------------------
    public bool IsActive
    {
      get { return this.isActive; }
      set { this.isActive = value; }
    } // IsActive

    // ----------------------------------------------------------------------
    // members
    private bool isActive;

  } // class Customer

  // ------------------------------------------------------------------------
  public class CustomerActiveColumn : ImageGridViewColumn
  {
    // ----------------------------------------------------------------------
    public CustomerActiveColumn()
    {
    } // CustomerActiveColumn

    // ----------------------------------------------------------------------
    protected override ImageSource GetImageSource( object value )
    {
      Customer customer = value as Customer;
      if ( customer != null )
      {
        return new BitmapImage( new Uri( customer.IsActive ? "Active.png" :
            "Inactive.png" ) );
      }
      return null;
    } // GetImageSource

  } // class CustomerActiveColumn

Points of Interest

At the core of layouting the ListView control lies the ListViewLayoutManager with the following responsibilities:

  • Preventing resizing columns of type Fixed and Proportional
  • Enforcing the range of allowed widths for type Range
  • Updating the column layout upon changes to the size of the ListView control
  • Updating the column layout upon changes to the widths of individual columns

To properly receive the required information, it is necessary to analyze the Visual Tree of the ListView control. The object Thumb provides the events for changes to the column width. To ensure correctly representing the mouse cursor, the events PreviewMouseMove and PreviewMouseLeftButtonDown are being handled.

The event ScrollChanged of the class ScrollViewer triggers updates due to control size changes. Only changes to the size of the control Viewport are relevant for resizing (ScrollChangedEventArgs.ViewportWidthChange).

Tracking the property Width of class GridViewColumn using DependencyPropertyDescriptor gives notification of changes to column widths.

To support integration in existing systems, the required column data is kept in Attached Properties. Using the method DependencyProperty.ReadLocalValue() allows detecting whether the own property is present in an object.

The class ConverterGridViewColumn uses a simple Binding and at the same time represents the converter (interface IValueConverter).

The class ImageGridViewColumn uses the FrameworkElementFactory in a DataTemplate to embed the image dynamically. By default, the images in the ListView/GridView control will be stretched automatically (property Image.Stretch). Because the image in a DataTemplate gets created dynamically, the property value of the template element must be assigned using a Binding:

 // ----------------------------------------------------------------------
 protected ImageGridViewColumn( Stretch imageStretch )
 {
   FrameworkElementFactory imageElement = new FrameworkElementFactory( typeof( Image ) );

   // image stretching
   Binding imageStretchBinding = new Binding();
   imageStretchBinding.Source = imageStretch;
   imageElement.SetBinding( Image.StretchProperty, imageStretchBinding );

   DataTemplate template = new DataTemplate();
   template.VisualTree = imageElement;
   CellTemplate = template;
 } // ImageGridViewColumn

History

  • 27th November, 2008
    • Fixed memory leak by un-registering the ListView events - thanks Dave and John
  • 3rd November, 2008
    • Fixed infinite recursion of column resizing in some rare cases - thanks ascalonx
    • Fixed column resizing in case of nested ListView - thanks mburi
  • 19th September, 2008
    • Fill column can now be at any position - the first occurrence will be used
  • 12th June, 2008
    • Added RangeColumn.IsFillColumn to fill the last column to the available visible area
  • 3rd June, 2008
    • Considering limits of range column during auto-resize (mouse double click) - thanks paul
  • 14th May, 2008
    • Fixed horizontal scrolling in case of absent proportional columns - thanks vernarim
  • 10th May, 2008
  • 23rd April, 2008
    • Added screenshot descriptions
    • Minor bug fixes
  • 6th April, 2008
    • Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jani Giannoudis


Member
In 1989, I started programming using Visual Basic, Turbo Pascal, Assembler, Clipper and C. From 1994 to 2001, I was as head of development of a Win32-CAD system in C++. During a few mandates as project manager from 2002 to 2003, I started with .NET using C#.

In 2004, I established the company Itenso GmbH with assistance and development of software systems in different business domains. The solutions are based on different Microsoft Technologies like SQL-Server, VSTO, C#, ASP.NET, WPF.
Occupation: Software Developer (Senior)
Company: Itenso GmbH
Location: Switzerland Switzerland

Other popular Grid & Data Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 71 (Total in Forum: 71) (Refresh)FirstPrevNext
QuestionEvents on the columns Pinmembere.tzumer21:55 21 Jun '09  
AnswerRe: Events on the columns PinmemberJani Giannoudis3:24 23 Jun '09  
GeneralPrinting WPF Listview which has 7-segment display user control as one of the columns Pinmembervidya bhat23:53 27 May '09  
AnswerRe: Printing WPF Listview which has 7-segment display user control as one of the columns PinmemberJani Giannoudis6:39 28 May '09  
QuestionIsFillColumn with Window.SizeToContent=WidthAndHeight Pinmemberbrinko9923:34 17 May '09  
AnswerRe: IsFillColumn with Window.SizeToContent=WidthAndHeight PinmemberJani Giannoudis4:43 18 May '09  
GeneralRe: IsFillColumn with Window.SizeToContent=WidthAndHeight Pinmemberbrinko995:57 18 May '09  
GeneralInvalidate a special cell in a RangeColumn PinmemberBurning Shadow6:08 16 Nov '08  
AnswerRe: Invalidate a special cell in a RangeColumn PinmemberJani Giannoudis3:41 17 Nov '08  
GeneralRe: Invalidate a special cell in a RangeColumn PinmemberBurning Shadow4:32 17 Nov '08  
AnswerRe: Invalidate a special cell in a RangeColumn PinmemberJani Giannoudis8:21 17 Nov '08  
GeneralRe: Invalidate a special cell in a RangeColumn PinmemberBurning Shadow20:44 8 Dec '08  
GeneralWow! PinmemberPaul Conrad19:28 10 Nov '08  
AnswerRe: Wow! PinmemberJani Giannoudis19:52 10 Nov '08  
GeneralRe: Wow! PinmemberPaul Conrad19:59 10 Nov '08  
GeneralGetting control Garbage Collected PinmemberViolinist22:02 4 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberJani Giannoudis20:13 4 Nov '08  
GeneralRe: Getting control Garbage Collected PinmemberViolinist22:20 26 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberJani Giannoudis2:32 26 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberViolinist23:08 26 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberJani Giannoudis20:11 26 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberViolinist222:44 26 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberJani Giannoudis23:48 26 Nov '08  
GeneralRe: Getting control Garbage Collected Pinmemberyohoohoy5:40 19 Nov '08  
AnswerRe: Getting control Garbage Collected PinmemberJani Giannoudis22:10 28 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2008
Editor: Sean Ewington
Copyright 2008 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project