Hi,

How do I add a subitem to a listview in WPF? I'm coding in VB and I do not see any subitem in it.

Thanks

Recommended Answers

All 3 Replies

Drop ListView control and use following code:

Window1.xaml.vb

Imports System.Collections.ObjectModel
Class Window1
    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    End Sub
    Private Sub Window1_Loaded_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim g As New GridView
        Dim col1 As New GridViewColumn
        Dim col2 As New GridViewColumn

        col1.Header = "No"
        col2.Header = "Name"
        col1.DisplayMemberBinding = New System.Windows.Data.Binding("No")
        col2.DisplayMemberBinding = New System.Windows.Data.Binding("Name")
        g.Columns.Add(col1)
        g.Columns.Add(col2)
        ListView1.ItemsSource = New Students
        ListView1.View = g
    End Sub
End Class

'
' Model class
Public Class Std
    Private mno As Integer
    Private mname As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal mno As Integer, ByVal mname As String)
        Me.mno = mno
        Me.mname = mname
    End Sub
    Public Property No() As Integer
        Get
            Return mno
        End Get
        Set(ByVal value As Integer)
            mno = value
        End Set
    End Property
    Public Property Name() As String
        Get
            Return mname
        End Get
        Set(ByVal value As String)
            mname = value
        End Set
    End Property
End Class

'
' Itemsource class
Public Class Students
    Inherits ObservableCollection(Of Std)
    Public Sub New()
        Add(New Std(1, "Rajesh"))
        Add(New Std(2, "Ram"))
        Add(New Std(3, "Krishana"))
    End Sub
End Class

My ListView control is not bound. I did code this way.

Dim lvi As New System.Windows.Forms.ListViewItem
        For Each blk In blks
            If blk.IsXRef Then
                Dim intIndex As Integer = Array.IndexOf(Src, blk.Name.ToString)
                If intIndex >= 0 Then
                    lvi.Text = blk.Name
                    lvi.SubItems(0).Text = "test"
                    LV.Items.Add(lvi)
                End If
            End If
        Next

But the result I get in the ListView is

ListViewItem:{test} ListViewItem:{test}

What could be wrong?

Your question was : How do I add a subitem to a listview in WPF?

ListView1.View = View.Details
        ListView1.Columns.Add("No")

        Dim str() As String = {"aa", "bb", "cc"}

        For Each s As String In str
            ListView1.Items.Add(s)
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.