Home » WPFRSS

How do I populate listview in 2 columns from 2 arrays in WPF VB.net?

I am using Visual Studio 2010 (WPF). I have a few texts stored on 2 arrays. I want to populate first column with 1st array and 2nd column with 2nd array. But I can barely use this list view control. Its easy in windows forms. But I want it in WPF form.

Suppose, 1st array is "Company" and 2nd array is "LTP".

The two column headers of the list view control will be "Company" and "Value"

Tell me the code for creating these 2 columns and then populate these with the 2 arrays. Plus, I will also have 2 text boxes in which I want the selected items texts. First box will contain the selected text in 1st column and second box will contain the selected text of 2nd column.

P.S. If list view control is not suitable for this then forget it and mention the name of the most suitable control with codes.

 

7 Answers Found

 

Answer 1

Hi,

You can try something like this:

With ListView1
 ' Loop through name array1
 For i = 0 To UBound(NameArray)
  .ListItems.Add i, ,nameArray1(i)
 Next
 ' Loop through name array2
 For i = 0 To UBound(SurnameArray)
  .ListItems(i).ListSubItems.Add i, , nameArray2(i)
 Next
 End With 
 

Answer 2

Hello Alam,

Thanks for your post.

I have moved your thread to WPF forum where you may get more better and quicker support because more WPF experts live there. Thanks for your understanding.

Best regards,
Liliane
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
 

Answer 3

I am using Visual Studio 2010 (WPF). I have a few texts stored on 2 arrays. I want to populate  first column with 1st array and 2nd column with 2nd array. But I can barely use this list view control. Its easy in windows forms. But I want it in WPF form.

Suppose, 1st array is "Company" and 2nd array is "LTP".

The two column headers of the list view control will be "Company" and "Value"

Tell me the code for creating these 2 columns  and then populate these with the 2 arrays. Plus, I will also have 2 text boxes in which I want the selected items texts. First box will contain the selected text in 1st column and second box will contain the selected text of 2nd column.

P.S. If list view control is not suitable for this then forget it and mention the name of the most suitable control with codes.

 

Answer 4

Hi sparrow10,

WPF ItemsControl can only get one ItemsSource, so we cannot set two arrays  for one ItemsControl. But we can create a class that contains two properties map the these two arrays value. And then bind the collection of this class to the ListView:

Imports System.Collections.ObjectModel

Class MainWindow

 PublicSubNew()

  ' This call is required by the designer.
  InitializeComponent()

  ' Add any initialization after the InitializeComponent() call.Dim companys AsString()
  Dim values AsInteger()
  companys = {"MS", "IBM", "Apple"}
  values = {100, 99, 98}

  Dim Items As ObservableCollection(Of Item) = New ObservableCollection(Of Item)()
  ForEach company AsStringIn companys
   Items.Add(New Item() With {.Company = company})
  NextFor Index AsInteger = 0 To Items.Count - 1
   Items(Index).Value = values(Index)
  Next
  listview.ItemsSource = Items

 EndSubEndClassPublicClass Item
 PublicProperty Company AsStringPublicProperty Value AsIntegerEndClass

 

XAML:

<Grid><ListViewx:Name="listview"><ListView.View><GridView><GridView.Columns><GridViewColumnHeader="Company"DisplayMemberBinding="{Binding Company}"/><GridViewColumnHeader="Value"DisplayMemberBinding="{Binding Value}"/></GridView.Columns></GridView></ListView.View></ListView></Grid>

It is not recommend to set several different arrays/lists to an ItemsControl, since we can not assure the length of all arrays/lists are the same.

Sincerely,

Bob Bao

 

Answer 5

Thanks. It works great.

But still, there's one thing left. I have a 2 textblocks. I want the selected items in listview  control to be on those textblocks. The item selected from the 1st column will be on textblock1 and the 2nd column will be on textblock2. Please give me the language in VB.net WPF.

 

Answer 6

Hi Alam,

Well, if you have set a databinding for the listview  and bound a List data on it. Then you could bind the TextBlock.Text property to the ListView.SelectedItem (here the SelectedITem is a instance of the List value):

<StackPanel><ListViewx:Name="listview"Height="100"><ListView.View><GridView><GridView.Columns><GridViewColumnHeader="Company"DisplayMemberBinding="{Binding Company}"/><GridViewColumnHeader="Value"DisplayMemberBinding="{Binding Value}"/></GridView.Columns></GridView></ListView.View></ListView><StackPanelOrientation="Horizontal"><TextBlockText="Text 1: "/><TextBlockText="{Binding ElementName=listview, Path=SelectedItem.Company}"/></StackPanel><StackPanelOrientation="Horizontal"><TextBlockText="Text 2: "/><TextBlockText="{Binding ElementName=listview, Path=SelectedItem.Value}"/></StackPanel></StackPanel>

The first TextBlock.Text is bound to the SelectedItem.Company, and other is bound the second property of the Item class. It is coded without behind-code. If you want to set them by VB.Net code, please use below code:

XAML:

<StackPanelOrientation="Horizontal"><TextBlockText="Text 1: "/><TextBlockx:Name="txtCompany"/></StackPanel><StackPanelOrientation="Horizontal"><TextBlockText="Text 2: "/><TextBlockx:Name="txtValue"/></StackPanel>

VB.Net

PublicSubNew()

  ' This call is required by the designer.
  InitializeComponent()

  ' Add any initialization after the InitializeComponent() call.
  ......

  Dim firstBinding AsNew Binding("SelectedItem.Company") With {.ElementName = "listview"}
  BindingOperations.SetBinding(txtCompany, TextBlock.TextProperty, firstBinding)

  Dim secondBinding AsNew Binding("SelectedItem.Value") With {.ElementName = "listview"}
  BindingOperations.SetBinding(txtValue, TextBlock.TextProperty, secondBinding)
 EndSub

Sincerely,

Bob Bao

 

Answer 7

Hey Bob Bao,

Thanks a lot. Your code works like a magic every time.

Another thing. Do you think there's a way to unbind this textblock from listview  with code?? I mean vb code not XAML.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter