ListView の GridView を作成するいくつかのコンバーターでこれを行いました。列名と (文字列にフォーマットされた) 列データを配列に読み取り、これらのコンバーターを使用しました。
<ValueConversion(GetType(String()), GetType(GridView))>
Public Class TableToGridViewConverter
Implements IValueConverter
Private Shared ReadOnly ItemConverter As New ArrayToItemConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim header() As String = TryCast(value, String())
Dim result As New GridView
If header IsNot Nothing Then
For i As Integer = 0 To header.Length - 1
result.Columns.Add(New GridViewColumn() With {.Header = header(i), .DisplayMemberBinding = New Binding() With {.Converter = ItemConverter, .ConverterParameter = i}})
Next
End If
Return result
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException
End Function
End Class
<ValueConversion(GetType(String()), GetType(String))>
Public Class ArrayToItemConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim result As String = String.Empty
If TypeOf value Is String() AndAlso IsNumeric(parameter) Then
Dim values() As String = value
Dim index As Integer = CInt(parameter)
If index >= 0 And index < values.Length Then
result = values(index)
End If
End If
Return result
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException
End Function
End Class
秘訣は、配列内の各列に対して GridViewColumn を作成し、値の配列にバインドするコンバーターを使用し、列インデックスを ConverterParameter として使用してそれぞれの列の値を選択することです。次のように使用します。
<ListView x:Name="TableDataListView" View="{Binding Path=DisplayTable.Header, Converter={StaticResource tableToGridView}}" ItemsSource="{Binding Path=DisplayRows}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
ここで、バインドされたプロパティ DisplayTable.Header と DisplayRows には、列名を含む文字列配列と、実際のデータを含む文字列配列の配列がそれぞれ含まれます。