How do I take a basic class that I've made and extract it's properties in the list in WPF? I've already tried playing with the itemssource property but nothing seems to have worked for me.
Public Class PageContent
Public StartDate As DateTime
Public Header As String
Public Content As String
End Class
<ListView Name="grid_PageContentList" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding StartDate}" Width="50"></GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Header}" Width="50"></GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Content}" Width="50"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You cannot bind directly to fields, you need to use properties instead:
Public Class PageContent
Public Property StartDate As DateTime
Public Property Header As String
Public Property Content As String
End Class
Also you need to ensure you have set you DataContext
. If you have just a simple form with a code behind approach then put this in the constructor or Loaded
handler:
Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loaded
MyData = New List(Of PageContent)
Dim pc As New PageContent
pc.Header = "AAAAAAAA"
pc.StartDate = Now
MyData.Add(pc)
pc = New PageContent
pc.Header = "BBBBBB"
pc.StartDate = Now.AddMinutes(1)
MyData.Add(pc)
DataContext = Me
End Sub
Public Property MyData() As List(Of PageContent)
End Class
And set your ItemsSource
in your XAML:
<ListView Name="grid_PageContentList" ItemsSource="{Binding MyData}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding StartDate}" Width="50"></GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Header}" Width="50"></GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Content}" Width="50"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You need to make that an ObservableCollection with INPC or a DependencyProperty. Don't just bind regular properties like that as you might not see the data.