Mega Code Archive

 
Categories / Silverlight / UI Controls
 

ListBox data binding

<UserControl x:Class='SilverlightApplication3.MainPage'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'      xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:d='http://schemas.microsoft.com/expression/blend/2008'      xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'      mc:Ignorable='d'      d:DesignWidth='640'      d:DesignHeight='480'>     <StackPanel x:Name="LayoutRoot">         <TextBlock x:Name="TitlePrompt" Text="Title:  " />         <TextBlock x:Name="Title" Text="{Binding Title, Mode=OneWay }" />         <TextBlock x:Name="AuthorPrompt" Text="Author:" />         <TextBlock x:Name="Author" Text="{Binding Author, Mode=OneWay }" />         <TextBlock x:Name="ChapterPrompt" Text="Chapters:  "/>         <ListBox x:Name="Chapters" ItemsSource="{Binding  Chapters, Mode=OneWay}"/>         <TextBlock x:Name="MultipleAuthorPrompt" Text="Multiple authors?:  "/>         <CheckBox x:Name="MultipleAuthor" IsChecked="{Binding MultipleAuthor, Mode=TwoWay}" />         <TextBlock x:Name="QOHPrompt" Text="Quantity On Hand:" />         <TextBox x:Name="QuantityOnHand" Text="{Binding QuantityOnHand, Mode=TwoWay}"/>         <Button x:Name="Change" Content="Change Book"/>     </StackPanel> </UserControl> //File:Page.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.Generic; using System.ComponentModel; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; namespace SilverlightApplication3 {    public class Book : INotifyPropertyChanged    {       private string bookTitle;       private string bookAuthor;       private int quantityOnHand;       private bool multipleAuthor;       private string authorURL;       private string authorWebPage;       private List<string> myChapters;       public event PropertyChangedEventHandler PropertyChanged;       public string Title       {          get { return bookTitle; }          set          {             bookTitle = value;             NotifyPropertyChanged( "Title" );          }       }          public string Author       {          get { return bookAuthor; }          set          {             bookAuthor = value;             NotifyPropertyChanged( "Author" );          }       }       public List<string> Chapters       {          get { return myChapters; }          set          {             myChapters = value;             NotifyPropertyChanged( "Chapters" );          }       }       public bool MultipleAuthor       {          get { return multipleAuthor; }          set          {             multipleAuthor = value;             NotifyPropertyChanged( "MultipleAuthor" );          }       }       public int QuantityOnHand       {          get { return quantityOnHand; }          set          {             quantityOnHand = value;             NotifyPropertyChanged( "QuantityOnHand" );          }       }       public void NotifyPropertyChanged( string propertyName )       {          if ( PropertyChanged != null )          {             PropertyChanged( this,new PropertyChangedEventArgs( propertyName ) );          }       }    }     public partial class MainPage : UserControl    {       private Book b1;       private Book b2;       private Book currentBook;       public MainPage()       {          InitializeComponent();          Loaded += new RoutedEventHandler( Page_Loaded );       }       void Page_Loaded( object sender,RoutedEventArgs e )       {          Change.Click += new RoutedEventHandler( Change_Click );          b1 = new Book();          InitBookB( b1 );          currentBook = b2 = new Book();          InitBookA( b2 );          SetDataSource();       }       private void InitBookA( Book b )       {          b.Title = "title";          b.Author = "author";          b.MultipleAuthor = true;          b.QuantityOnHand = 20;          b.Chapters = new List<string>() { "1", "2", "3", "4" };       }       private void InitBookB( Book b )       {          b.Title = "title 2";          b.Author = "A";          b.MultipleAuthor = false;          b.QuantityOnHand = 150;          b.Chapters = new List<string>(){"A", "B", "C"};       }       void Change_Click( object sender, RoutedEventArgs e )       {          if ( currentBook == b1 )             currentBook = b2;          else             currentBook = b1;          SetDataSource();       }       void SetDataSource()       {          LayoutRoot.DataContext = currentBook;       }    } }