21
第 1 第 21 第第 1.0 第第 WinForms 第 WPF 第第 GUI 第第第第 在在在在在 在在在在在 ,: 在在 WPF 在在在在在在在 第第

在本单元中,您将学习: 探索 WPF 数据绑定的概念

Embed Size (px)

DESCRIPTION

目标. 在本单元中,您将学习: 探索 WPF 数据绑定的概念. 绑定到 CLR 对象. ObjectDataProvider 类: 使您能够直接将 CLR 对象用作 XAML 代码的绑定源。 提供以下属性以查询对象并绑定到结果: ConstructorParameters :使您能够通过 XAML 将值传递到对象的构造函数。 MethodName :使您能够调用对象中定义的方法。 MethodParameters :使您能够将参数传递到使用 MethodName 属性调用的方法。  - PowerPoint PPT Presentation

Citation preview

第 1 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

在本单元中,您将学习:探索 WPF 数据绑定的概念

目标

第 2 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

ObjectDataProvider 类:使您能够直接将 CLR 对象用作 XAML 代码的绑定源。 提供以下属性以查询对象并绑定到结果:

ConstructorParameters :使您能够通过 XAML 将值传递到对象的构造函数。MethodName :使您能够调用对象中定义的方法。MethodParameters :使您能够将参数传递到使用 MethodName 属性调用的方法。 IsAsynchronous :使您能够声明是在主线程中还是在工作程序线程中创建对象。

 

绑定到 CLR 对象

第 3 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段创建 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 对象(续)

第 4 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段创建 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 对象(续)

将创建名为 lst 的列表集合,并向它填充类构造函数中的学生姓名。

第 5 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段创建 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 对象(续)

定义用于返回姓名列表的 GetNames() 方法。

第 6 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段使用 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 对象(续)

第 7 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段使用 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 对象(续)

定义数据源。

第 8 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段使用 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 对象(续)

指定用于绑定的源对象。

第 9 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段使用 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 对象(续)

调用 GetNames() 方法以获取数据。

第 10 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段使用 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 对象(续)

将 ListBox 控件绑定到数据源。

第 11 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

下图显示了上述代码段的输出。

绑定到 CLR 对象(续)

第 12 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

ObservableCollection 类:提供动态数据集合,它总是在底层数据源发生更改时发送通知并自动更新 UI 。 驻留在 System.Collections.ObjectModel 名称空间中。 继承自 Collection 类,该类是所有泛型集合的基类。实现 INotifyPropertyChanged 和 INotifyCollectionChanged 接口。

绑定到 CLR 对象(续)

第 13 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

INotifyPropertyChanged 接口:确定控件属性值的更改。每当对集合中任何项的属性进行更新时将引发 PropertyChanged 事件。INotifyCollectionChanged 接口:确定集合是否已更改。 每当对集合中的任何项进行更新时将引发 CollectionChanged 事件。

绑定到 CLR 对象(续)

第 14 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段创建 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 对象(续)

第 15 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段创建 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 对象(续)

返回 ObservableCollection 对象 BookData 。

第 16 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

以下代码段将 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 对象(续)

第 17 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

<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 对象(续)

第 18 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

下图显示了上述代码段的输出。

绑定到 CLR 对象(续)

第 19 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

问题陈述:John 是 StarShine Technologies Pvt Ltd 的开发人员,当前正在进行小型音像店的项目。该商店有一个老电影集合并将它们租给用户。电影的详细列表(如电影名称、男演员、女演员、导演)存储在名为 MovieList 的对象中。 John 被要求设计该应用程序。这将使商店主管能够查看带有状态的电影列表。 此外,它应该提供“添加”和“删除”按钮,这些按钮将使主管能够向集合添加和删除记录。

活动 1 :绑定到集合

第 20 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

解决方案:要为视频商店设计应用程序, John 需要执行以下任务:

创建 WPF 应用程序。设计应用程序的用户界面。在代码隐藏文件中添加功能。执行应用程序并验证输出。

活动 1 :绑定到集合(续)

第 21 张(共 21 张)

版本 1.0

使用 WinForms 和 WPF 开发 GUI 应用程序

在本单元中,您学习了:TwoWay 数据绑定可以使用以下类来实现:

ObjectDataProvider 类ObservableCollection 类

小结