【问题标题】:How to add item in Listview on WPF如何在 WPF 的 Listview 中添加项目
【发布时间】:2019-04-13 21:29:42
【问题描述】:

我可以在ListView 中添加列,但是在ListView 中添加项目非常困难。这是我的代码:

    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

我刚刚创建了ListViewItem.add,然后使用subitem 连续添加更多项目。但现在不同了。如何在 WPF 中的 listview 中添加项目而不创建新类,因为数据列的数量是动态的。

编辑:

我已经在互联网上冲浪了几个小时,但没有找到任何帮助。 在任何地方,它都是使用具有预定义列数的类来完成的,而我想根据应该具有不同 Ipnumber 列的数据库添加列项。

【问题讨论】:

  • 创建行就像写myGridView.ItemsSource = myEmployeeCollection一样简单。只需使用Data Binding
  • itemssource 属性未找到 :(
  • 哦,你使用的是旧的ListView 模式。我明白了,您的示例直接来自MSDN。继续阅读 MSDN 然后,它包含您需要的所有信息。
  • 我已经读过了..没有找到向列表视图添加项目的指南:(

标签: wpf vb.net listview


【解决方案1】:

您应该在代码中初始化对象集合,例如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

然后你可以简单地将它绑定到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>

要动态初始化列,您可以添加问题中的代码。如果您从数据库中获取数据,这没有什么区别。只需填写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

这样 XAML 将如下所示。

<ListView Name="ListView1" ItemsSource="{Binding Path=Employees}"/>

【讨论】:

  • 谢谢,但是..看看 xaml。该列是预定义的..我想要的是,根据数据库表动态添加列,并根据数据库添加记录..不是预定义的列.. :(
  • 正在开发的应用程序正在使用 ADODB 经典编辑数据库表,列表视图作为记录集位置导航器..
【解决方案2】:
Dim row As String() = New String() {"John", "Doe", "1"}
myGridView.Rows.Add(row)

这就是你要找的吗?

【讨论】:

  • @adjen towfeek btw,我正在使用 LISTVIEW。我使用您的代码如下:code Dim row As String() = New String() {"John", "Doe", "1" } listview.Items.Add(row)code 不起作用
【解决方案3】:

你的程序的 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>

而且你的 VB 部分必须是这样的:

Listview.Items.Add(New With {Key .PencereSN = "some string", Key .PencereAD = "some string"})

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    相关资源
    最近更新 更多