Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i am adding data from two text boxes into a listbox,i want to add the data in horizontal manner.suppose i add data from two text boxes i want them to be displayed in horizontal manner,and for the next entry they should be displayed below the previous entry.and also can a header be provided for the data to display what that data meant to be.(i know it can be done using list view, but i want to know whether it can be done using listbox)

name code <---header
sam 060
rob 061

1 solution

Do like this :

//Create two classes

Class Person
private String name;
public String Name
get{return name;}

private int code;
public int Code
get{return code;}

public Person(String name,int code)

Class PersonCollection:ObservableCollection<person>


//Go into the code-behind of your application
// Add this in your constructor

public Window1
PersonCollection perColl=new PersonCollection();

//In the Add button event handler(that adds the value in ListBox), do this

perColl.Add(new Person(txtName.Text,int.Parse(txtCode.Text));</person>

Move over to your XAML :

<listbox name="listBox">
<stackpanel orientation="Horizontal">
<textblock text="{Binding" path="Name}/">
<textblock text="{Binding" path="Code}/">

The Stackpanel above in your DataTemplate will keep the name and code in a horizontal way.

Hope it helped!
Share this answer
Sergey Alexandrovich Kryukov 23-Mar-11 17:39pm    
I'm confused. Will it make ListBox multi-column? How?
Tarun.K.S 24-Mar-11 4:06am    
Listbox can't be made multicolumn, for that ListView is a better choice. OP wanted the former way. :)
So with the DataTemplate, the data in each row will be displayed like this : Name Code.
Sergey Alexandrovich Kryukov 24-Mar-11 4:30am    
Thank you. I was confused because both are mentioned. I vote 5.
Tarun.K.S 24-Mar-11 4:34am    
Thanks a lot SA! :)
girish sp 24-Mar-11 3:17am    
@SAKryukov-u can do it..but datasource should be involved..but i forgot to mention no datasource is involved in my appln..can it be done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900