Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ListView with Footer

0.00/5 (No votes)
25 Jun 2012 1  
A listview control with footer.

Introduction

A footer row with the column totals frozen at the bottom is a fairly common item on grids. You can probably get one by using a DataGrid, but there’s a good deal of complexity associated with DataGrids you might want to avoid.

Using the Code

I made a Custom Control ListViewF based on the standard ListView. You need to supply the ListViewF with a FooterObj which should be the same type of object you are displaying in the regular rows. You can specify a FooterStyle. Both FooterObj and FooterStyle can be instantiated in the XAML.

You will need to call the ListViewF.InitFooter function after the columns have loaded. The function creates a CellTemplateSelector for each GridViewColumn.

If the column is using DisplayMemberBinding it creates two CellTemplates – one for the regular cells and one for the footer. The regular cell template is just a TextBlock with the Text bound to whatever the DisplayMemberBinding was. The footer is the same but wrapped in a Border. The original DisplayMemberBinding is cleared so the DataTemplateSelector can be used.

If the column is using a CellTemplate it creates a footer template by adding a ContentPresenter with the original template as the ContentTemplate and then wrapping the ContentPresenter in a Border.

I don’t handle cases where the column already has a CellTemplateSelector. It shouldn’t be too hard to create a new selector that calls the original CellTemplateSelector and then either returns the original template or wraps it in a Border.

Points of Interest

You need to use a custom GridView that overrides the DefaultStyleKey. If you add columns using a normal GridView the Control goes back to using the regular ListView template.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here