[英]How to add item in Listview on WPF
I can add column in ListView
, but it is so difficult to add an item in ListView
. 我可以在
ListView
添加列,但是很难在ListView
添加项目。 Here is my code: 这是我的代码:
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
listview.View = myGridView
I just created ListViewItem.add
and then used the subitem
to add more items in a row. 我刚刚创建
ListViewItem.add
,然后使用subitem
来连续添加更多项目。 But now it's different. 但是现在不一样了。 How can I add item in
listview
in WPF WITHOUT creating a new class, because the number of the columns of data is dynamic. 如何在WPF中的
listview
中添加项目而不创建新类,因为数据列的数量是动态的。
EDIT: 编辑:
I had been surfing the internet for hours but found nothing of great help. 我已经上网冲浪了几个小时,但发现没有什么帮助。 Everywhere, it is done using class with predefined numbers of column, while I want to add column item based on database which should have different Ipnumber of columns.
在任何地方,都可以使用具有预定义列数的类来完成此操作,而我想基于应该具有不同Ipnumber列的数据库添加列项目。
You should initialize the object collection in your code, for example Employees
. 您应该在代码中初始化对象集合,例如
Employees
。
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
End Class
And then you can simply bind it to the ListView
: 然后,您只需将其绑定到
ListView
:
<ListView ItemsSource="{Binding Path=Employees}">
<ListView.View>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Employee Information">
<GridViewColumn Header="First Name" Width="100" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Header="Last Name" Width="100" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Header="Employee No." Width="100" DisplayMemberBinding="{Binding Path=EmployeeNumber}"/>
</GridView>
</ListView.View>
</ListView>
To initialize columns dynamically, you can add the code from your question. 要动态初始化列,您可以添加问题中的代码。 It doesn't make difference if you are taking data from database.
如果要从数据库中获取数据,这没有什么区别。 Just fill in
Employees
collection and bind it to the ListView
. 只需填写
Employees
集合并将其绑定到ListView
。
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
SetGridViewDynamically()
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
Private Sub SetGridViewDynamically()
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
myGridView.Columns.Add(gvc3)
ListView1.View = myGridView
End Sub
End Class
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
This way XAML will look like this. 这样,XAML将看起来像这样。
<ListView Name="ListView1" ItemsSource="{Binding Path=Employees}"/>
Dim row As String() = New String() {"John", "Doe", "1"}
myGridView.Rows.Add(row)
Is this what you're looking for? 这是您要找的东西吗?
XAML part of your program must be like be this: 您程序的XAML部分必须是这样的:
<ListView x:Name="lvPencere" HorizontalAlignment="Left" Height="156" Grid.Row="1" VerticalAlignment="Top" Width="309">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="PencereSN" Width="0" DisplayMemberBinding="{Binding PencereSN}"/>
<GridViewColumn Header="Pencere Adı" Width="300" DisplayMemberBinding="{Binding PencereAD}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
And your VB part must be like this: 而且您的VB部分必须是这样的:
Listview.Items.Add(New With {Key .PencereSN = "some string", Key .PencereAD = "some string"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.