新建一个类:Book
using System.Collections.Generic;
using System.ComponentModel;
namespace GlobalStyles
{
public class Book : INotifyPropertyChanged
{
private string bookTitle;
private string bookAuthor;
private int quantityOnHand;
private bool isPublished;
private List<string> myChapters;
// implement the required event for the interface
public event PropertyChangedEventHandler PropertyChanged;
public string Title
{
get { return bookTitle; }
set
{
bookTitle = value;
NotifyPropertyChanged("Title");
} // end set
} // end property
public string Author
{
get { return bookAuthor; }
set
{
bookAuthor = value;
NotifyPropertyChanged("Author");
} // end set
}
public List<string> Chapters
{
get { return myChapters; }
set
{
myChapters = value;
NotifyPropertyChanged("Chapters");
}
}
public bool IsPublished
{
get { return isPublished; }
set
{
isPublished = value;
NotifyPropertyChanged("IsPublished");
} // end set
}
public int QuantityOnHand
{
get { return quantityOnHand; }
set
{
quantityOnHand = value;
NotifyPropertyChanged("QuantityOnHand");
} // end set
}
// factoring out the call to the event
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
} // end class
}
设计页面:
<UserControl x:Class="GlobalStyles.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="False">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition MaxHeight="30" />
<RowDefinition MaxHeight="30" />
<RowDefinition MaxHeight="70" />
<RowDefinition MaxHeight="30" />
<RowDefinition MaxHeight="30" />
<RowDefinition MaxHeight="40" />
<RowDefinition MaxHeight="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="TitlePrompt" Text="Title: "
Style="{StaticResource TextBlockPrompt}"
Grid.Row="1" Grid.Column="1" />
<TextBlock x:Name="Title"
Text="{Binding Title, Mode=OneWay }"
Style="{StaticResource TextBlockValue}"
Grid.Row="1" Grid.Column="2" />
<TextBlock x:Name="AuthorPrompt" Text="Author: "
Style="{StaticResource TextBlockPrompt}"
Grid.Row="2" Grid.Column="1" />
<TextBlock x:Name="Author" Text="{Binding Author, Mode=OneWay }"
Style="{StaticResource TextBlockValue}"
Grid.Row="2" Grid.Column="2" />
<TextBlock x:Name="ChapterPrompt" Text="Chapters"
Style="{StaticResource TextBlockPrompt}"
Grid.Row="3" Grid.Column="1" />
<ListBox x:Name="Chapters"
ItemsSource="{Binding Chapters, Mode=OneWay}"
Style="{StaticResource ListBoxStyle}"
Grid.Row="3" Grid.Column="2" />
<TextBlock x:Name="PublishedPrompt"
Text="Published?: "
Style="{StaticResource TextBlockPrompt}"
Grid.Row="4" Grid.Column="1" />
<CheckBox x:Name="Literature"
Content="Publshsed"
IsChecked = "{Binding IsPublished, Mode=TwoWay}"
Style="{StaticResource CheckBoxStyle}"
FontFamily="Comic Sans MS"
Grid.Row="4" Grid.Column="2"/>
<TextBlock x:Name="QOHPrompt"
Text="Quantity On Hand: "
Style="{StaticResource TextBlockPrompt}"
Foreground="Red"
FontWeight="Bold"
Grid.Row="5" Grid.Column="1" />
<TextBox x:Name="QuantityOnHand"
Text="{Binding QuantityOnHand, Mode=TwoWay}"
Style="{StaticResource TextBoxStyle}"
Grid.Row="5" Grid.Column="2" />
<Button x:Name="Change" Content="Change Book"
Style="{StaticResource Button}"
Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" />
</Grid>
</UserControl>
关键的页面代码:
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;
namespace GlobalStyles
{
public partial class Page : UserControl
{
private Book b1; // now a class member
private Book b2;
private Book currentBook;
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender,
RoutedEventArgs e)
{
// event handler for change button
Change.Click +=
new RoutedEventHandler(Change_Click);
// initial book values
b1 = new Book();
InitializeBleak(b1);
currentBook = b2 = new Book();
InitializeProgramming(b2);
SetDataSource();
}
private void InitializeProgramming(Book b)
{
b.Title = "Programming Silverlight";
b.Author = "Jesse Liberty";
b.IsPublished = false;
b.QuantityOnHand = 20;
b.Chapters = new List<string>() { "Introduction", "Controls", "Events", "Styles" };
}
private void InitializeBleak(Book b)
{
b.Title = "Bleak House";
b.Author = "Charles Dickens";
b.IsPublished = false;
b.QuantityOnHand = 150;
b.Chapters = new List<string>() { "In Chancery", "In Fashion", "A Progress", "Telescoopic Philanthropy",
"A Morning Adventure", "Quite at Home", "The Ghosts Walk", "Covering Sins", "Signs and Tokens", "The Law Writer"};
}
void Change_Click(object sender, RoutedEventArgs e)
{
if (currentBook == b1)
currentBook = b2;
else
currentBook = b1;
SetDataSource();
}
void SetDataSource()
{
LayoutRoot.DataContext = currentBook;
//Title.DataContext = currentBook;
//Author.DataContext = currentBook;
//Chapters.DataContext = currentBook;
//MultipleAuthor.DataContext = currentBook;
//QuantityOnHand.DataContext = currentBook;
}
}
}