210 likes | 383 Views
目标. 在本单元中,您将学习: 探索 WPF 数据绑定的概念. 绑定到 CLR 对象. ObjectDataProvider 类: 使您能够直接将 CLR 对象用作 XAML 代码的绑定源。 提供以下属性以查询对象并绑定到结果: ConstructorParameters :使您能够通过 XAML 将值传递到对象的构造函数。 MethodName :使您能够调用对象中定义的方法。 MethodParameters :使您能够将参数传递到使用 MethodName 属性调用的方法。
E N D
目标 • 在本单元中,您将学习: • 探索 WPF 数据绑定的概念
绑定到 CLR 对象 • ObjectDataProvider 类: • 使您能够直接将 CLR 对象用作 XAML 代码的绑定源。 • 提供以下属性以查询对象并绑定到结果: • ConstructorParameters:使您能够通过 XAML 将值传递到对象的构造函数。 • MethodName:使您能够调用对象中定义的方法。 • MethodParameters:使您能够将参数传递到使用 MethodName 属性调用的方法。 • IsAsynchronous:使您能够声明是在主线程中还是在工作程序线程中创建对象。
绑定到 CLR 对象(续) • 以下代码段创建stringData 类: class stringData { List<string> lst = new List<string>(); public stringData() { lst.Add("Matthew"); lst.Add("John"); lst.Add("Sam"); lst.Add("Carrol"); } public List<string> GetNames() {return lst;} }
绑定到 CLR 对象(续) • 以下代码段创建 stringData 类: class stringData { List<string> lst = new List<string>(); public stringData() { lst.Add("Matthew"); lst.Add("John"); lst.Add("Sam"); lst.Add("Carrol"); } public List<string> GetNames() {return lst;} } 将创建名为 lst 的列表集合,并向它填充类构造函数中的学生姓名。
绑定到 CLR 对象(续) • 以下代码段创建 stringData 类: class stringData { List<string> lst = new List<string>(); public stringData() { lst.Add("Matthew"); lst.Add("John"); lst.Add("Sam"); lst.Add("Carrol"); } public List<string> GetNames() {return lst;} } 定义用于返回姓名列表的 GetNames() 方法。
绑定到 CLR 对象(续) • 以下代码段使用 ObjectDataProvider 对象将 stringData 类中的数据绑定到列表框控件: <Grid> <Grid.Resources> <ObjectDataProvider ObjectType="{x:Type local:stringData}" x:Key="objstring" MethodName="GetNames"/> </Grid.Resources> <ListBox Height="64" ItemsSource="{Binding Source={StaticResource objstring}}"/> </Grid>
绑定到 CLR 对象(续) • 以下代码段使用 ObjectDataProvider 对象将 stringData 类中的数据绑定到列表框控件: <Grid> <Grid.Resources> <ObjectDataProvider ObjectType="{x:Type local:stringData}" x:Key="objstring" MethodName="GetNames"/> </Grid.Resources> <ListBox Height="64" ItemsSource="{Binding Source={StaticResource objstring}}"/> </Grid> 定义数据源。
绑定到 CLR 对象(续) • 以下代码段使用 ObjectDataProvider 对象将 stringData 类中的数据绑定到列表框控件: <Grid> <Grid.Resources> <ObjectDataProvider ObjectType="{x:Type local:stringData}" x:Key="objstring" MethodName="GetNames"/> </Grid.Resources> <ListBox Height="64" ItemsSource="{Binding Source={StaticResource objstring}}"/> </Grid> 指定用于绑定的源对象。
绑定到 CLR 对象(续) • 以下代码段使用 ObjectDataProvider 对象将 stringData 类中的数据绑定到列表框控件: <Grid> <Grid.Resources> <ObjectDataProvider ObjectType="{x:Type local:stringData}" x:Key="objstring" MethodName="GetNames"/> </Grid.Resources> <ListBox Height="64" ItemsSource="{Binding Source={StaticResource objstring}}"/> </Grid> 调用 GetNames() 方法以获取数据。
绑定到 CLR 对象(续) • 以下代码段使用 ObjectDataProvider 对象将 stringData 类中的数据绑定到列表框控件: <Grid> <Grid.Resources> <ObjectDataProvider ObjectType="{x:Type local:stringData}" x:Key="objstring" MethodName="GetNames"/> </Grid.Resources> <ListBox Height="64" ItemsSource="{Binding Source={StaticResource objstring}}"/> </Grid> 将 ListBox 控件绑定到数据源。
绑定到 CLR 对象(续) • 下图显示了上述代码段的输出。
绑定到 CLR 对象(续) • ObservableCollection 类: • 提供动态数据集合,它总是在底层数据源发生更改时发送通知并自动更新 UI。 • 驻留在System.Collections.ObjectModel名称空间中。 • 继承自Collection 类,该类是所有泛型集合的基类。 • 实现INotifyPropertyChanged 和 INotifyCollectionChanged接口。
绑定到 CLR 对象(续) • INotifyPropertyChanged接口: • 确定控件属性值的更改。 • 每当对集合中任何项的属性进行更新时将引发PropertyChanged事件。 • INotifyCollectionChanged接口: • 确定集合是否已更改。 • 每当对集合中的任何项进行更新时将引发CollectionChanged事件。
绑定到 CLR 对象(续) • 以下代码段创建 observable 集合类 BookData: class BookData { public string Title { get; set; } public string Author { get; set; } public string Publisher { get; set; } public static ObservableCollection<BookData> GetAllBooks() { ObservableCollection<BookData> books = new ObservableCollection<BookData>(); books.Add(new BookData() { Title = "World of Warcraft", Author = "Azure Browne", Publisher="Blizzard"}); return books;}}
绑定到 CLR 对象(续) • 以下代码段创建 observable 集合类 BookData: class BookData { public string Title { get; set; } public string Author { get; set; } public string Publisher { get; set; } public static ObservableCollection<BookData> GetAllBooks() { ObservableCollection<BookData> books = new ObservableCollection<BookData>(); books.Add(new BookData() { Title = "World of Warcraft", Author = "Azure Browne", Publisher="Blizzard"}); return books;}} 返回 ObservableCollection 对象 BookData。
绑定到 CLR 对象(续) • 以下代码段将 BookData 数据源绑定到 TextBlock 控件: <Window.Resources> <ObjectDataProvider x:Key="books" ObjectType="{x:Type d:BookData}" MethodName="GetAllBooks"/> </Window.Resources> <Grid> <TextBlock Text="{Binding Source={StaticResource books},Path=Title}“ />
绑定到 CLR 对象(续) <TextBlock Text="{Binding Source={StaticResource books},Path=Author}"/> <TextBlock Text="{Binding Source={StaticResource books},Path=Publisher}" Height="20" Width="60" Margin="12,64,431,227" /> </Grid>
绑定到 CLR 对象(续) • 下图显示了上述代码段的输出。
问题陈述: John 是 StarShine Technologies Pvt Ltd 的开发人员,当前正在进行小型音像店的项目。该商店有一个老电影集合并将它们租给用户。电影的详细列表(如电影名称、男演员、女演员、导演)存储在名为MovieList的对象中。John 被要求设计该应用程序。这将使商店主管能够查看带有状态的电影列表。 此外,它应该提供“添加”和“删除”按钮,这些按钮将使主管能够向集合添加和删除记录。 活动 1:绑定到集合
解决方案: 要为视频商店设计应用程序,John 需要执行以下任务: 创建 WPF 应用程序。 设计应用程序的用户界面。 在代码隐藏文件中添加功能。 执行应用程序并验证输出。 活动 1:绑定到集合(续)
在本单元中,您学习了: TwoWay 数据绑定可以使用以下类来实现: ObjectDataProvider 类 ObservableCollection 类 小结