【发布时间】:2016-03-26 09:10:56
【问题描述】:
已解决感谢克莱门斯。我只需要在我的类定义中的“Public”一词之后添加“Property”属性:
在 VB.NET 中,我正在尝试制作一个非常简单的 Datagrid 示例(即没有数据库连接的示例等)。但无论我尝试什么,Datagrid 始终显示正确的行数和更正的列数(以及正确的列标题),但单元格是空的:
Public Class GeneratedImage
Public Property AssignmentId As Integer
Public Property DocumentPageNumber As Integer
Public Property FullPathToImage As String
Public Sub New(id As Integer, pg As Integer, docpath As String)
AssignmentId = id
DocumentPageNumber = pg
FullPathToImage = docpath
End Sub
End Class
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim myList As New List(Of GeneratedImage)
myList.Add(New GeneratedImage(1, 1, "c:\nowhere"))
myList.Add(New GeneratedImage(2, 2, "c:\nowhere2"))
GeneratedImagesInformationDatagrid.ItemsSource = myList
' GeneratedImagesInformationDatagrid.DataContext = myList
end sub
然后,在 XAML 中,我按如下方式绑定:
<DataGrid x:Name="GeneratedImagesInformationDatagrid"
Height="500"
VerticalAlignment="Top"
ItemsSource="{Binding}"
Width="300">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DocumentPageNumber}"
Header="Pg" />
<DataGridTextColumn Binding="{Binding Path=AssignmentId}"
Header="Assignment Id" />
<DataGridTextColumn Binding="{Binding Path=FullPathToImage}"
Header="Full Path To Image" />
</DataGrid.Columns>
</DataGrid>
我尝试过使用和不使用“ItemsSource="{Binding}" 字段。
任何帮助表示赞赏!
【问题讨论】:
-
AssignmentId、DocumentPageNumber 和 FullPathToImage 必须是 properties,而不是字段。 WPF 数据绑定仅适用于公共属性。为简单起见,请使用 auto-implemented properties。
-
谢谢你,克莱门斯!那成功了。我在网上找了很多例子,一定没有注意到“属性”属性。正如您所建议的,我还研究了自动实现的属性。谢谢!
标签: wpf vb.net xaml data-binding datagrid