configuring data binding part1 abto software lecture korotchyn
TRANSCRIPT
![Page 1: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/1.jpg)
Windows Application Development with Microsoft .NET Framework 4
Configuring Data Binding
1. Binding to Data Sources2. Manipulating and Displaying Data
by Oleksiy Korotchyn
![Page 2: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/2.jpg)
2
Binding to a List
There are two scenarios to binding to list:
Binding a single element to a list and enabling navigation of that list
Binding a collection (such as the items in ListBox) to a list so that the list’s elements are all displayed at one time.
![Page 3: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/3.jpg)
3
Binding an Item Control to a List
Item controls have built-in properties that enable data binding (Data-Related Properties).
![Page 4: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/4.jpg)
4
Binding an Item Control to a List
Data-Related Properties of Item control:
1) ItemsSource – Represents the collection that contains the items that make up the source of the list. You set this property to a Binding object that is bound to the appropriate collection.
2) DisplayMemberPath – indicates the property of the bound collection that will be used to create the display text for each item.
![Page 5: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/5.jpg)
5
Binding an Item Control to a List
Data-Related Property of Item control:
3) IsSynchronizedWithCurrentItem – determines whether the selected item is kept synchronized with the CurrentItem property in the Items collection.
![Page 6: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/6.jpg)
6
Binding an Item Control to a List
Data-Related Property of Item control:
4) ItemTemplate – the data template used to create the visual appearance of each item.
![Page 7: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/7.jpg)
7
Binding an Item Control to a List
<ListBox
ItemsSource="{Binding Source={StaticResource myList}}"
DisplayMemberPath="FirstName"
/>
![Page 8: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/8.jpg)
8
Binding an Item Control to a List
Bind a ListBox control to an object that is created at run time:
grid1.DataContext = myCustomers;
<Grid Name="grid1">
<ListBox
ItemsSource="{Binding}"
DisplayMemberPath="CustomerName"
/>
</Grid>
![Page 9: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/9.jpg)
9
Binding an Item Control to a List
Let’s see example
![Page 10: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/10.jpg)
10
Binding a Single Property to a List
grid1.DataContext = myCustomers;
<Grid Name="grid1">
<Label Content="{Binding /FirstName}"/>
<Label Content="{Binding Path = FirstName}"/>
</Grid>
![Page 11: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/11.jpg)
11
Navigating a Collection or List
WPF has a built-in navigation mechanism for data and collections.
When a collection is bound to a WPF binding, an ICollectionView interface is created behind the scenes.
![Page 12: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/12.jpg)
12
Navigating a Collection or List
ICollectionView Members Involved in Navigation:
1) CurrentItem – this property returns the current item.
2) CurrentPosition – this property returns the numeric position of the current item.
![Page 13: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/13.jpg)
13
Navigating a Collection or List
ICollectionView Members Involved in Navigation:
3) IsCurrentAfterLast – this property indicates whether the current item is after the last item in the collection.
4) IsCurrentBeforeFirst – this property indicates whether the current item is before the first item in the collection.
![Page 14: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/14.jpg)
14
Navigating a Collection or List
ICollectionView Members Involved in Navigation:
5) MoveCurrentToFirst – this method sets the current item to the first item in the collection.
6) MoveCurrentToLast – this method sets the current item to the last item in the collection.
![Page 15: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/15.jpg)
15
Navigating a Collection or List
ICollectionView Members Involved in Navigation:
7) MoveCurrentTo – this method sets the current item to the indicated item.
8) MoveCurrentToPosition – this method sets the current item to the item at the designated position.
![Page 16: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/16.jpg)
16
Navigating a Collection or List
To get a reference to ICollectionView:
System.ComponentModel.ICollectionView myView;
myView = CollectionViewSource.GetDefaultView(myCollection);
![Page 17: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/17.jpg)
17
Navigating a Collection or List
CollectionViewSource.GetDefaultView returns an ICollectionView object that is actually one of three classes, depending on the class of the source collection:
If the source collection implements IBindingList, the view returned is a BindingListCollectionView object.
If the source collection implements IList but not I BindingList, the view returned is a ListCollectionView object.
If the source collection implements IEnumerable but not IList or IBindingList, the view returned is a CollectionView object.
![Page 18: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/18.jpg)
18
Navigating a Collection or List
Let’s see example
![Page 19: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/19.jpg)
19
Binding to ADO.NET Objects
Binding to ADO.NET objects is basically the same as binding to any other collection or list.
![Page 20: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/20.jpg)
20
Binding to Hierarchical Data (Binding to Related ADO.NET Tables)
When binding to lists of complex objects, you might want to create a master-detail view that enables the user to select one item from an upper-level list and view the details about the selected object.
When hierarchical data is retrieved from databases, it typically is presented in related tables.
{Binding Path=ParentTable/Relation/Relation2}
![Page 21: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/21.jpg)
21
Binding to Hierarchical Data (Binding to Related ADO.NET Tables)
Let’s see example
![Page 22: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/22.jpg)
22
Binding to an Object with ObjectDataProvider
The ObjectDataProvider class enables you to bind a WPF element or property to a method called on an object.
You can specify an object type and a method on that type, then bind to the results of that method call.
![Page 23: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/23.jpg)
23
Binding to an Object with ObjectDataProvider
Important Properties of ObjectDataProvider
1) ConstructorParameters – represents the list of parameters to pass to the constructor.
2) MethodParameters – represents the list of parameters to pass to the method specified by the MethodName property.
3) MethodName – represents the name of the method of the source object to call.
4) ObjectType – gets or sets the type of object of which to create an instance.
5) IsAsynchronous – indicates whether object creation and method calls are performed on the foreground thread or on a background thread.
6) ObjectInstance – gets or sets the object used as the binding source.
![Page 24: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/24.jpg)
24
Binding to an Object with ObjectDataProvider
Let’s see example
![Page 25: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/25.jpg)
25
Binding to XML using XmlDataProvider
The XmlDataProvider class enables you to bind WPF elements to data in the XML format
![Page 26: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/26.jpg)
26
Binding to XML using XmlDataProvider
Important Properties of XmlDataProvider
1) Document – gets or sets the XmlDocument object to be used as the binding source.
2) Source – gets or sets the Uniform Resource Indicator (URI) of the XML file to be used as the binding source.
3) XPath – gets or sets the XPath query used to generate the XML data.
![Page 27: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/27.jpg)
27
Binding to XML using XmlDataProvider
<Window.Resources>
<XmlDataProvider x:Key="Items">
<x:XData>
<Workspace xmlns="" Name="Workspace">
<Project Name="Project 1" />
<Project Name="Project 2" />
<Project Name="Project 3" />
</Workspace>
</x:XData>
</XmlDataProvider>
</Window.Resources>
![Page 28: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/28.jpg)
28
Binding to XML using XmlDataProvider
Using XPath with XmlDataProvider
<Window.Resources>
<XmlDataProvider x:Key="Items" Source="Items.xml" XPath="Items/ExpensiveItems" />
</Window.Resources>
<ListBox
ItemsSource="{Binding Source={StaticResource Items} XPath=Diamond}"
DisplayMemberPath="ItemName"
/>
![Page 29: Configuring Data Binding part1 ABTO Software Lecture Korotchyn](https://reader033.vdocuments.site/reader033/viewer/2022052901/556cc97cd8b42aba548b501c/html5/thumbnails/29.jpg)
29
Binding to XML using XmlDataProvider
Let’s see example