Question : WPF, VB.net, generating a multi column listbox from  list (of type Structure)

Hi Experts,

I am new to WPF.  I want to display a MULTI COLUMN listbox or listview or datagrid/table of some sort. The data should come from a generic list (of type Structure).  As a stubborn programmer, I prefer to apply my ASP.net patterns to this new WPF paradigm, if at all possible.

Here is how I would generate the data in ASP.net:

       response.write ("<table>")
        myList = myEmp.getStructEmployeeList("SEN", "Active")
        For Each item In myList
            response.write("<tr>")
            response.write("<td>" & item.LastName & "</td>")
            response.write("<td>" & item.FirstName& "</td>")
            response.write("<td>" & item.City & "</td>")
            response.write("<td>" & item.State & "</td>")
            response.write("<tr>")
        Next
       response.write ("</table>")

      'The method get.StructEmployeeList returns EmployeeList -  List(Of Employees_Structure)
         
    Public Structure Employees_Structure
        Public EmployeeNumber As Integer
        Public LastName As String
        Public FirstName As String
        Public Initial As Char
        Public HomeAddress As String
        Public HomeAddress2 As String
        Public City As String
        Public State As String
        Public ZipCode As String
       etc...
  End Structure

I was able to use this for-each-loop pattern and generate a SINGLE column listbox in WPF.

        myList = myEmp.getStructEmployeeList("SEN", "Active")
        For Each item In myList
            ListBox1.Items.Add(item.LastName & ", " & item.FirstName & " (" & item.EmployeeNumber & ")")
        Next

But I can't seem to generate anything with multiple columns in WPF.

Thanks,


Answer : WPF, VB.net, generating a multi column listbox from  list (of type Structure)

Hi pttoy;

This should do what you want.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Dim width As Double = (ListBox1.ActualWidth / 3) - 2

For Each es As Employees_Structure In myEmp
    ' Create a new ListBox item for es
    Dim li As New ListBoxItem
    ' Create a Stack panel 
    Dim sp As New StackPanel
    sp.Orientation = Orientation.Horizontal
    sp.HorizontalAlignment = HorizontalAlignment.Stretch
    ' Create a TextBlock to place the info for a column in ListBox
    ' one for each column
    Dim tb1 As New TextBlock
    tb1.Text = es.LastName
    tb1.Width = width
    Dim tb2 As New TextBlock
    tb2.Text = es.FirstName
    tb2.Width = width
    Dim tb3 As New TextBlock
    tb3.Text = es.State
    tb3.Width = width
    ' Add the TextBox to the stack panel
    sp.Children.Add(tb1)
    sp.Children.Add(tb2)
    sp.Children.Add(tb3)
    ' Add the stack panel to the list box item
    li.Content = sp
    ' Add the list box item to the list box
    ListBox1.Items.Add(li)
Next
Random Solutions  
 
programming4us programming4us