我想将DataGrid绑定到包含嵌套集合的对象集合,因为每个行的值的数量在运行时都是未定义的。
我有一个名为 Record
的课程Public Class Record
Private _timestamp As DateTime
Private _deviceName As String
Private _values As List(Of Double)
Public Property Timestamp As DateTime
Get
Return _timestamp
End Get
Set(value As DateTime)
_timestamp = value
End Set
End Property
Public Property DeviceName As String
Get
Return _deviceName
End Get
Set(value As String)
_deviceName = value
End Set
End Property
Public Property Values As List(Of Double)
Get
Return _values
End Get
Set(value As List(Of Double))
_values = value
End Set
End Property
Public Sub New(ByVal timestamp As DateTime, ByVal deviceName As String, ByVal values As List(Of Double))
Me.Timestamp = timestamp
Me.DeviceName = deviceName
Me.Values = values
End Sub
End Class
以及记录对象
的集合Dim records As ObservableCollection(Of Record)
records = New ObservableCollection(Of Record)
With records
.Add(New Record(Now, "DEV1", New List(Of Double) From {0.1, 0.2, 0.3}))
.Add(New Record(Now, "DEV2", New List(Of Double) From {1.1, 1.2, 1.3}))
.Add(New Record(Now, "DEV3", New List(Of Double) From {2.1, 2.2, 2.3}))
End With
我不知道如何将来自属性值的值绑定到XAML中的列。
我应该如何将属性绑定到XAML datagrid的列?标题怎么样?我该怎么设置它们?
我想要获得的是
+------------+--------+--------+--------+--------+
| DATE | DEVICE | VALUE1 | VALUE2 | VALUE3 |
+------------+--------+--------+--------+--------+
| 2014/10/17 | DEV1 | 0.1 | 0.2 | 0.3 |
| 2014/10/17 | DEV2 | 1.1 | 1.2 | 1.3 |
| 2014/10/17 | DEV3 | 2.1 | 2.2 | 2.3 |
+------------+--------+--------+--------+--------+
编辑根据Mike的建议,我实现了一个ITypedList接口。 类 Datalog 实现ITypedList,是 DataLogRecord 对象的ArrayList。 DataLogRecord 对象有2个属性(Timestamp和DeviceName),第三个属性是 DataLogValueCollection (即 DataLogValue 的ArrayList)。
Imports System.ComponentModel
Public Class DataLog
Inherits ArrayList
Implements ITypedList
Private Function GetItemProperties(ByVal listAccessors() As PropertyDescriptor) As PropertyDescriptorCollection Implements ITypedList.GetItemProperties
Return TypeDescriptor.GetProperties(GetType(DataLogRecord))
End Function
Private Function GetListName(ByVal listAccessors() As PropertyDescriptor) As String Implements ITypedList.GetListName
Return "DataLogRecord"
End Function
End Class
Public Class DataLogRecord
Private _timestamp As DateTime
Private _deviceName As String
Private _values As DataLogValueCollection
Public Property Timestamp As DateTime
Get
Return _timestamp
End Get
Set(value As DateTime)
_timestamp = value
End Set
End Property
Public Property DeviceName As String
Get
Return _deviceName
End Get
Set(value As String)
_deviceName = value
End Set
End Property
Public Property Values As DataLogValueCollection
Get
Return _values
End Get
Set(value As DataLogValueCollection)
_values = value
End Set
End Property
Public Sub New(ByVal timestamp As DateTime, ByVal deviceName As String,
ByVal values As DataLogValueCollection)
Me.Timestamp = timestamp
Me.DeviceName = deviceName
Me.Values = values
End Sub
End Class
Imports System.ComponentModel
Public Class DataLogValueCollection
Inherits ArrayList
Implements ITypedList
Private Function GetItemProperties(ByVal listAccessors() As PropertyDescriptor) As PropertyDescriptorCollection Implements ITypedList.GetItemProperties
Return TypeDescriptor.GetProperties(GetType(DataLogValue))
End Function
Private Function GetListName(ByVal listAccessors() As PropertyDescriptor) As String _
Implements ITypedList.GetListName
Return "DataLogValue"
End Function
End Class
Public Class DataLogValue
Private _value As Nullable(Of Double) = Nothing
Private _name As String = Nothing
Public Property Value As Nullable(Of Double)
Get
Return _value
End Get
Set(value As Nullable(Of Double))
_value = Value
End Set
End Property
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = Value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal value As Nullable(Of Double), ByVal name As String)
Me.Value = value
Me.Name = name
End Sub
End Class
网格定义
<DataGrid Name="DataGridValues" Height="Auto" Width="Auto"
AutoGenerateColumns="True"
Style="{StaticResource DataGridStyle}"
CellStyle="{StaticResource DataGridCellEventStyle}"
HorizontalAlignment="Stretch"
ItemsSource="{Binding DataLog}">
</DataGrid>
现在结果DataGrid是
+------------+------------+--------+--------+--------+
| Timestamp | DeviceName | Values |
+------------+------------+--------+--------+--------+
| 2014/10/17 | DEV1 | (Collection) |
| 2014/10/17 | DEV2 | (Collection) |
| 2014/10/17 | DEV3 | (Collection) |
+------------+------------+--------+--------+--------+
正确创建前2列,而值(来自DataLogValueCollection)显示为单列。我错过了什么?