Wpf 非常简单的Datagrid示例不起作用
解决了多亏了克莱门斯。我只需要在类定义中的“Public”之后添加“Property”属性:Wpf 非常简单的Datagrid示例不起作用,wpf,vb.net,xaml,data-binding,datagrid,Wpf,Vb.net,Xaml,Data Binding,Datagrid,解决了多亏了克莱门斯。我只需要在类定义中的“Public”之后添加“Property”属性: Public Class GeneratedImage Public Property AssignmentId As Integer Public Property DocumentPageNumber As Integer Public Property FullPathToImage As String . . etc 在VB.N
Public Class GeneratedImage
Public Property AssignmentId As Integer
Public Property DocumentPageNumber As Integer
Public Property FullPathToImage As String
.
. etc
在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}”字段。感谢您的帮助!感谢Clemens解决了问题。我只需要在我的类定义中的“Public”一词后添加“Property”属性:
Public Class GeneratedImage
Public Property AssignmentId As Integer
Public Property DocumentPageNumber As Integer
Public Property FullPathToImage As String
.
. etc
等等。AssignmentId、DocumentPageNumber和FullPathToImage必须是,而不是字段。WPF数据绑定仅适用于公共属性。为简单起见,请使用。谢谢你,克莱门斯!这就成功了。我在网上查找了许多示例,一定没有注意到“属性”“属性。正如您所建议的,我还研究了自动实现的属性。非常感谢。