【发布时间】:2018-05-27 09:26:31
【问题描述】:
如何通过Entity Framework/Linq绑定添加ListViewItems?
我的 ListView 在带有绑定的 xaml 中:
<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>
这是我的代码
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
编辑:尝试了ObserverableCollection,但现在我得到了NullReferenceException!
如果我调试,地址得到数据..不为空
【问题讨论】:
-
在 C# 中,您有 ObservableCollection,这是您在 .xaml 上链接为 itemSource 的内容。不确定它在 vb 中是如何工作的,但应该有类似的东西。当您不使用 itemSource 时,您必须告诉 Dispatcher 使用 INotifyPropertyChanged 更新列表,但同样,我只从 C# 中知道,但可能会为您提供搜索解决方案的线索。
-
@Nekeniehl ObservableCollection 是一个 .NET Framework 类,因此支持所有语言。
-
@Raizzen "link as itemSource on the .xaml" 意味着您应该将 ListView 的
ItemsSource属性绑定到您的项目类的ObservableCollection。当您编写DisplayMemberBinding="{Binding address}"时,项目类应该有一个名为address的公共属性。 -
就像我说的我对vb一无所知,但总是学习东西的好时机,谢谢!
-
@Nekeniehl, Clemens ok ObserverableCollection.. 试试看
标签: wpf vb.net linq xaml binding