1

How to add ListView Items via binding by Entity Framework / Linq?

I got my ListView in the xaml with bindings here:

<ListView x:Name="lstvw_Overview" HorizontalAlignment="Left" Height="310" Margin="11,89,0,0" VerticalAlignment="Top" Width="676">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Adresse" DisplayMemberBinding="{Binding address}"/>
            </GridView>
        </ListView.View>
</ListView>

This is my code

Public Sub New()
    Initialize()
End Sub
Dim address As String
Dim items As ObservableCollection(Of Uebersicht)
Public Structure Uebersicht
    Private _address As String
    Public Property address As String
        Get
            Return _address
        End Get
        Set(value As String)
            _address = value
        End Set
    End Property
End Structure
Sub Initialize()
    InitializeComponent()
    fillListView()
End Sub
 Sub fillListView()
    Using container As New infrastrukturDB_TESTEntities1
        Dim mailAddressList = From tbl_unzustellbarAdressen In container.tbl_unzustellbarAdressen
        For Each mail In mailAddressList
            address = mail.unzustellbarMail.ToString()
            Try
                items.Add(New Uebersicht With {.address = address})
            Catch ex As Exception
                MessageBox.Show("Error")
            End Try

        Next
    End Using
End Sub

EDIT: tried the ObserverableCollection but now i got a NullReferenceException! If i debug, address got data.. not null

6
  • In C# you have the ObservableCollection which is what you link as itemSource on the .xaml. Not sure how its working in vb, but there should be something similar. When you are not using the itemSource, you have to tell the Dispatcher to update the list with INotifyPropertyChanged, but again, I only know from C#, but maybe give you clues for searching a solution. Commented Dec 13, 2017 at 13:14
  • @Nekeniehl ObservableCollection is a .NET Framework class, and hence available in all supported languages. Commented Dec 13, 2017 at 13:19
  • @Raizzen "link as itemSource on the .xaml" means that you should bind the ItemsSource property of the ListView to an ObservableCollection of your item class. When you write DisplayMemberBinding="{Binding address}" the item class is supposed to have a public property named address. Commented Dec 13, 2017 at 13:22
  • Like I said I do not have idea about vb, but always is a good time to learn things, thanks! Commented Dec 13, 2017 at 13:23
  • @Nekeniehl, Clemens ok ObserverableCollection.. gonna try it out Commented Dec 13, 2017 at 13:24

1 Answer 1

4

Since you are adding strings to the ListView, you should not bind to the address property but the source object itself:

<ListView x:Name="lstvw_Overview" HorizontalAlignment="Left" Height="310" Margin="11,89,0,0" VerticalAlignment="Top" Width="676">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Adresse" DisplayMemberBinding="{Binding}"/>
        </GridView>
    </ListView.View>
</ListView>

Edit: You need to intialize the ObservableCollection before you can add items to it. And to be able to bind to the ObservableCollection, you must expose it as a property:

Public Sub New()
    Initialize()
End Sub

Dim address As String

Private _items As ObservableCollection(Of Uebersicht) = New ObservableCollection(Of Uebersicht) 
Public Property Items As ObservableCollection(Of Uebersicht)
    Get
        Return _items
    End Get
    Set(value As ObservableCollection(Of Uebersicht))
        _items = value
    End Set
End Property

Sub Initialize()
    InitializeComponent()
    DataContext = Me
    fillListView()
End Sub

XAML:

<ListView ItemsSource="{Binding Items}" HorizontalAlignment="Left" Height="310" Margin="11,89,0,0" VerticalAlignment="Top" Width="676">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Adresse" DisplayMemberBinding="{Binding address}"/>
        </GridView>
    </ListView.View>
</ListView>
Sign up to request clarification or add additional context in comments.

9 Comments

ah u again.. xD but only with Binding in the xaml, how should the program know which variable to choose
ok now it works. i just get the wrong output in my gui but i think i can fix it by myself. iam sry for annoying u iam still learning :(
in that version ´Private _items As ObservableCollection(Of Uebersicht) = New ObservableCollection(Of Uebersicht)´it underlines _items -> initializer for structur member is just valid for shared- member/constants
The setter should be Set(value As ObservableCollection(Of Uebersicht)).I edited my answer slightly.
it still underlines Private _items, btw what xaml is the right one? there are two in ur answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.