In Silverlight, the ListBox is a very powerful control. Summarizes how data is bound in the listbox.
First, create a new book class,
1 Public class Book2 {3 Public stringBookName {Get;Set; }4 5 Public stringAuthor {Get;Set; }6}
In the next BookViewModel.cs,
1 Public classBookviewmodel2 {3 PrivateList<book>Booklist;4 5 PublicBookviewmodel ()6 {7 This. Booklist =NewList<book>() { 8 NewBook () {bookname="Book1", author="Author1"},9 NewBook () {bookname="Book2", author="Author2"},Ten NewBook () {bookname="BOOK3", author="Author3"} One }; A } - - PublicList<book>Booklist the { - Get{return This. Booklist;} - } -}
In view, add the binding ViewModel
1 <UserControl.DataContext>2 <viewmodel:BookViewModel/>3 </ Usercontrol.datacontext>
There are two ways of binding a ListBox:
Way One:
1 <listbox name="listtmp" grid.row="1" grid.column="0" itemssource="{Binding Booklist,mode=twoway} " 2 Displaymemberpath="bookname"/>
Way two:
1<listbox grid.row="1"grid.column="0"Itemssource="{Binding Booklist,mode=twoway}">2<ListBox.ItemTemplate>3<DataTemplate>4<stackpanel orientation="Horizontal">5<textblock text="{Binding Bookname,mode=twoway}"/>6<textblock text="||"/>7<textblock text="{Binding Author,mode=twoway}"/>8</StackPanel>9</DataTemplate>Ten</ListBox.ItemTemplate> One</ListBox>
Above.