A common question I see in the msdn forums is How do I databind a Listview control on a windows form. Unfortunately the windows forms ListView does not support databinding. The wpf version does support databinding and it pretty host on a windows form. Please note you need to have the .Net Framework 3.0 or greater installed for this.
In VS 2008 you will have a WPF Interoperability tab in the toolbox. Drag a ElementHost on to your windows forms. In VS 2005 you will have to add the ElementHost to the toolbox.
For this example I am going to bind the ListView to the list of files in My Documents. I will be showing the name, size, and last access time.
So we need to create a ListView, a GridView to add to the ListView to show the columns, and 3 columns
Dim lstv As New System.Windows.Controls.ListView
Dim gv As New System.Windows.Controls.GridView
Dim gvcName As New System.Windows.Controls.GridViewColumn
Dim gvcSize As New System.Windows.Controls.GridViewColumn
Dim gvcLast As New System.Windows.Controls.GridViewColumn
For each column we have to set the header text, create a binding, and a FrameworkElementFactory to show the data .
With gvcName
.Header = "Name"
Dim bName As New System.Windows.Data.Binding
bName.Mode = Windows.Data.BindingMode.OneWay
bName.Path = New System.Windows.PropertyPath("Name")
Dim feName As New FrameworkElementFactory(GetType(System.Windows.Controls.TextBlock))
Dim dtName As New DataTemplate
feName.SetBinding(System.Windows.Controls.TextBlock.TextProperty, bName)
dtName.VisualTree = feName
.DisplayMemberBinding = bName
.CellTemplate = dtName
End With
Add the column to the GridView
gv.Columns.Add(gvcName)
Then add the GridView to the ListView
lstv.View = gv
Now we can bind the ListView to a file list and add it to the ElementHost.
lstv.ItemsSource = di.GetFiles
ElementHost1.Child = lstv
Full Listing of code
Imports System.Windows.Forms.Integration
Imports System.Windows
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim di As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
Dim lstv As New System.Windows.Controls.ListView
Dim gv As New System.Windows.Controls.GridView
Dim gvcName As New System.Windows.Controls.GridViewColumn
Dim gvcSize As New System.Windows.Controls.GridViewColumn
Dim gvcLast As New System.Windows.Controls.GridViewColumn
With gvcName
.Header = "Name"
Dim bName As New System.Windows.Data.Binding
bName.Mode = Windows.Data.BindingMode.OneWay
bName.Path = New System.Windows.PropertyPath("Name")
Dim feName As New FrameworkElementFactory(GetType(System.Windows.Controls.TextBlock))
Dim dtName As New DataTemplate
feName.SetBinding(System.Windows.Controls.TextBlock.TextProperty, bName)
dtName.VisualTree = feName
.DisplayMemberBinding = bName
.CellTemplate = dtName
End With
With gvcSize
.Header = "Length"
Dim bSize As New System.Windows.Data.Binding
bSize.Mode = Windows.Data.BindingMode.OneWay
bSize.Path = New System.Windows.PropertyPath("Length")
Dim feSize As New FrameworkElementFactory(GetType(System.Windows.Controls.TextBlock))
Dim dtSize As New DataTemplate
feSize.SetBinding(System.Windows.Controls.TextBlock.TextProperty, bSize)
dtSize.VisualTree = feSize
.DisplayMemberBinding = bSize
.CellTemplate = dtSize
End With
With gvcLast
.Header = "Last Access"
Dim bLast As New System.Windows.Data.Binding
bLast.Mode = Windows.Data.BindingMode.OneWay
bLast.Path = New System.Windows.PropertyPath("LastAccessTime")
Dim feLast As New FrameworkElementFactory(GetType(System.Windows.Controls.TextBlock))
Dim dtLast As New DataTemplate
feLast.SetBinding(System.Windows.Controls.TextBlock.TextProperty, bLast)
dtLast.VisualTree = feLast
.DisplayMemberBinding = bLast
.CellTemplate = dtLast
End With
gv.Columns.Add(gvcName)
gv.Columns.Add(gvcSize)
gv.Columns.Add(gvcLast)
lstv.View = gv
lstv.ItemsSource = di.GetFiles
ElementHost1.Child = lstv
End Sub
End Class