<UserControl.Resources>
<utils:StringArrayConverter x:Key="stringArrayConverter" />
<utils:BookTemplateSelector x:Key="bookTemplateSelector" />
<ObjectDataProvider x:Key="books" ObjectType="local:BookFactory" MethodName="GetBooks" />
<DataTemplate x:Key="wroxTemplate" DataType="{x:Type local:Book}">
<Border Background="Red" Margin="10" Padding="10">
<StackPanel>
<Label Content="{Binding Title}" />
<Label Content="{Binding Publisher}" />
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate x:Key="dummiesTemplate" DataType="{x:Type local:Book}">
<Border Background="Yellow" Margin="10" Padding="10">
<StackPanel>
<Label Content="{Binding Title}" />
<Label Content="{Binding Publisher}" />
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate x:Key="bookTemplate" DataType="{x:Type local:Book}">
<Border Background="LightBlue" Margin="10" Padding="10">
<StackPanel>
<Label Content="{Binding Title}" />
<Label Content="{Binding Publisher}" />
</StackPanel>
</Border>
</DataTemplate>
</UserControl.Resources>
<DockPanel DataContext="{StaticResource books}">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Center">
<Button Margin="5" Padding="4" Content="Add Book" Click="OnAddBook" />
</StackPanel>
<ListBox DockPanel.Dock="Left" ItemsSource="{Binding}" Margin="5" MinWidth="120" IsSynchronizedWithCurrentItem="True" ItemTemplateSelector="{StaticResource bookTemplateSelector}">
</ListBox>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Title" Grid.Row="0" Grid.Column="0" Margin="10,0,5,0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Label Content="Publisher" Grid.Row="1" Grid.Column="0" Margin="10,0,5,0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Label Content="Isbn" Grid.Row="2" Grid.Column="0" Margin="10,0,5,0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Label Content="Authors" Grid.Row="3" Grid.Column="0" Margin="10,0,5,0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox Text="{Binding Title}" Grid.Row="0" Grid.Column="1" Margin="5" />
<TextBox Text="{Binding Publisher}" Grid.Row="1" Grid.Column="1" Margin="5" />
<TextBox Text="{Binding Isbn}" Grid.Row="2" Grid.Column="1" Margin="5" />
<TextBlock Text="{Binding Authors, Converter={StaticResource stringArrayConverter}, ConverterParameter=', '}" Grid.Row="3" Grid.Column="1" Margin="5" VerticalAlignment="Center" TextWrapping="Wrap" />
</Grid>
</DockPanel>
BookFactory类
public class BookFactory
{
private ObservableCollection<Book> books = new ObservableCollection<Book>();
public BookFactory()
{
books.Add(new Book("Professional C# 4 with .NET 4", "Wrox Press", "978-0-470-50225-9", "Christian Nagel", "Bill Evjen", "Jay Glynn", "Karli Watson", "Morgan Skinner"));
books.Add(new Book("Professional C# 2008", "Wrox Press", "978–0-470-19137-8", "Christian Nagel", "Bill Evjen", "Jay Glynn", "Karli Watson", "Morgan Skinner"));
books.Add(new Book("Beginning Visual C# 2010", "Wrox Press", "978-0-470-50226-6", "Karli Watson", "Christian Nagel", "Jacob Hammer Pedersen", "Jon D. Reid", "Morgan Skinner", "Eric White"));
books.Add(new Book("Windows 7 Secrets", "Wiley", "978-0-470-50841-1", "Paul Thurrott", "Rafael Rivera"));
books.Add(new Book("C# 2008 for Dummies", "For Dummies", "978-0-470-19109-5", "Stephen Randy Davis", "Chuck Sphar"));
}
public Book GetTheBook()
{
return books[0];
}
public void AddBook(Book book)
{
books.Add(book);
}
public IEnumerable<Book> GetBooks()
{
return books;
}
}
Book类
public class Book : INotifyPropertyChanged
{
public Book(string title, string publisher, string isbn, params string[] authors)
{
this.title = title;
this.publisher = publisher;
this.isbn = isbn;
this.authors.AddRange(authors);
}
public Book()
: this("unknown", "unknown", "unknown")
{
}
public event PropertyChangedEventHandler PropertyChanged;
private string title;
public string Title {
get
{
return title;
}
set
{
title = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
private string publisher;
public string Publisher
{
get
{
return publisher;
}
set
{
publisher = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Publisher"));
}
}
private string isbn;
public string Isbn {
get
{
return isbn;
}
set
{
isbn = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Isbn"));
}
}
private readonly List<string> authors = new List<string>();
public string[] Authors
{
get
{
return authors.ToArray();
}
}
public override string ToString()
{
return this.title;
}
}
在XAML中以下Text Binding的内容是DockPanel中的DataContext,因为DataContext是Book的一个集合,所以默认是Binding到第一个Book上,当ListBox设置了 IsSynchronizedWithCurrentItem时,会改变DataContext的CurrentItem(即数据源当前选择的Item),所以当ListBox改变选择项的时候,TextBox和TextBlock的Text内容也会产生相应的变化。
<DockPanel DataContext="{StaticResource books}">
<TextBox Text="{Binding Title}" Grid.Row="0" Grid.Column="1" Margin="5" />
<TextBox Text="{Binding Publisher}" Grid.Row="1" Grid.Column="1" Margin="5" />
<TextBox Text="{Binding Isbn}" Grid.Row="2" Grid.Column="1" Margin="5" />
<TextBlock Text="{Binding Authors, Converter={StaticResource stringArrayConverter}, ConverterParameter=', '}" Grid.Row="3" Grid.Column="1" Margin="5" VerticalAlignment="Center" TextWrapping="Wrap" />