IsSynchronizedWithCurrentItem
来源:互联网 发布:淘宝装修服务平台 编辑:程序博客网 时间:2024/06/06 16:35
<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" />
- IsSynchronizedWithCurrentItem
- Combobox的IsSynchronizedWithCurrentItem 属性
- IsSynchronizedWithCurrentItem 会让 ListBox 默认选中第一项
- 不能执行已释放Script的代码
- 实现小球随机碰撞,最好分析
- 例题7.4.3
- 扮演你的角色
- PADS9.5 软件源及视频教程
- IsSynchronizedWithCurrentItem
- 可输入的select标签(input+select)
- Build Your Own PaaS on RHEL 6
- Android签名机制
- POJ1018:Communication System(枚举+搜索剪枝)
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- 在arc的工程调用非arc的类
- vs2010 调试无法命中断点。
- Quest 3d简单lua脚本实例