具有嵌套属性的DataGrid绑定集合

时间:2011-12-20 19:56:23

标签: wpf binding datagrid nested

我有一个Person和Address类。 Person类具有Name,Gender etc和Address等属性。 Address类具有Street,City等属性作为字符串。如果我有一个叫做“人”的人的集合,这是一个List(Of Person)。我将“people”绑定到WPF中的DataGrid。 “名称”列很好,但“城市”列始终为空。

Class Address
Sub New()
End Sub
Private _city As String
Property City() As String
Get
Return _city 
End Get
Set(ByVal value As String)
_city = vaule
End Set
End Property
End Class

Class Person
Sub New()
End Sub
Private _name As String = ""
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = vaule
End Set
End Property

Private _address As Address
Property Address() As Address
Get
Return _address 
End Get
Set(ByVal value As Address)
_address = vaule
End Set
End Property

End Class

在Window_Loaded事件下,我有

Dim people As List(of Person) = _DAL.GetAllPeople()
Me.myDataGrid.ItemsSource = people

Dim nameBinding As Binding = New Binding
nameBinding.Path = New PropertyPath("Name") 
Me.nameColumn.Binding = nameBinding 'This works fine in datagrid

Dim addressBinding As Binding = New Binding

addressBinding.Path = New PropertyPath("Address.City")
Me.cityColumn.Binding = addressBinding 'This does not work

XAML代码:

<DataGrid x:Name="myDataGrid" HorizontalAlignment="Left" Margin="8,177.4,0,38" Width="240" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" CanUserResize="False" IsReadOnly="True" Header="Name" Width="100"/>
<DataGridTextColumn x:Name="cityColumn" CanUserResize="False" Header="City" IsReadOnly="True" Width="*" />
</DataGrid.Columns>
</DataGrid>

0 个答案:

没有答案