Bind data to TextBlock
<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: " Style="{StaticResource Prompt}" />
<TextBlock x:Name="Title" Text="{Binding Title, Mode=OneWay }" />
<TextBlock x:Name="AuthorPrompt" Text="Author: " Style="{StaticResource Prompt}" />
<TextBlock x:Name="Author" Text="{Binding Author, Mode=OneWay }" />
<TextBlock x:Name="ChapterPrompt" Text="Chapters: " Style="{StaticResource Prompt}" />
<ListBox x:Name="Chapters" ItemsSource="{Binding Chapters, Mode=OneWay}"/>
<TextBlock x:Name="MultipleAuthorPrompt" Text="Multiple authors?: " Style="{StaticResource Prompt}" />
<CheckBox x:Name="MultipleAuthor" IsChecked="{Binding MultipleAuthor, Mode=TwoWay}" />
<TextBlock x:Name="QOHPrompt" Text="Quantity On Hand: " Style="{StaticResource Prompt}" />
<TextBox x:Name="QuantityOnHand" Text="{Binding QuantityOnHand, Mode=TwoWay}"/>
<Button Template="{StaticResource RoundButton}" Content="Hello" x:Name="Change" />
</StackPanel>
</UserControl>
//File:Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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.ComponentModel;
using System.Collections;
using System.Collections.Generic;
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 = "A";
b.Author = "B";
b.MultipleAuthor = true;
b.QuantityOnHand = 20;
b.Chapters = new List<string>() { "A", "Controls", "Events", "Styles" };
}
private void InitBookB( Book b )
{
b.Title = "Bleak House";
b.Author = "Charles Dickens";
b.MultipleAuthor = false;
b.QuantityOnHand = 150;
b.Chapters = new List<string>(){"1", "2", "3" };
}
void Change_Click( object sender, RoutedEventArgs e )
{
if ( currentBook == b1 )
currentBook = b2;
else
currentBook = b1;
SetDataSource();
}
void SetDataSource()
{
LayoutRoot.DataContext = currentBook;
}
}
}
Related examples in the same category