Data binding in a ListBox in Silverlight

Source: Internet
Author: User
Tags silverlight

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.