Upload
craig
View
237
Download
0
Embed Size (px)
Citation preview
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 1/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
WPF: Working With Data
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 2/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Objectives
• Set how to retrieve data from a SQL Server
database
• Create common data bound forms
• See how to display information from main and
related tables
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 3/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda
• Data Binding
• Using Views
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 4/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Data Binding
• Process of getting information from one object
to another and automatically displaying it in
one or more elements in user interface
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 5/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Without Data Binding
1. In window’s Loaded event handler, query
database and retrieve list of customers
2. In code or in XAML, set ItemsSource property
of list box to DataTable or other object that
contains customers
3. In list box’s SelectionChanged event handler,
query database and retrieve information for
selected customer
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 6/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Without Data Binding
4. In list box’s SelectionChanged event handler,
set Text property of each text box to value of
appropriate column of customer data
5. In Save button’s Click event handler, read
value of each text box and update appropriate
column of customer data
6. In Save button’s Click event handler, save
changes back to database
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 7/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
With Data Binding
1. In window’s Loaded event handler, query databaseand retrieve list of customers
2. In code or in XAML, set ItemsSource property of list box to DataTable or other object that contains
customers3. In list box’s SelectionChanged event handler, query
database and retrieve information for selectedcustomer
4. In Save button’s Click event handler, save changesback to database
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 8/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Data Source Options
• Same as in Windows Forms applications
• Many data sources
• Custom business objects
• ADO.NET DataSets• LINQ to SQL
• Entity Framework
• Services
• Web services
• Windows Communication Foundation (WCF) services
• WCF Data Services
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 9/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Binding to an Object
• Display clients in a list box
• Create collection of clients in code
• Populate the collection
• Bind to object in XAML
• <ListBox
• Name="listBox1"
• DisplayMemberPath="Name"ItemsSource=
• "{Binding ElementName=bindingToObject,Path=Clients}" />
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 10/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Binding to an Object
• Select a client and display details in data-bound textboxes in a grid
• Bind grid by setting data context to selected list box item
•
Text boxes in grid inherit data binding• <Grid Grid.Row="1" DataContext=
• "{Binding ElementName=listBox1,
• Path=SelectedItem}" >
• <TextBox Text="{Binding Path=Name}"
• Grid.Row="0" Grid.Column="1"
• Margin="5,5,10,5" MaxHeight="25" />
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 11/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Binding to a DataTable
• Display customers in a list box and details forselected customer in text boxes in grid
• Use TableAdapter or intermediary data layer to retrievecustomers and store in DataTable
• Set ItemsSource property of list box to Customers table
• Grid data context is set to list box selected item
• Text boxes automatically update when a customer is selected
•
List box SelectionChanged event handler runs query toretrieve orders for selected customer
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 12/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Binding to a LINQ Query
• Display customers in a list box and details forselected customer in text boxes in grid
• Run LINQ to SQL query to retrieve customers
• Set ItemsSource property of results of query• Grid data context is set to list box selected item
• Text boxes automatically update when a customer isselected
• List box SelectionChanged event handler runs LINQ to SQL query to retrieve orders for selectedcustomer
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 13/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Binding to One Row of Data
• Display information on a product when user
enters product id and clicks Find button
• Use TableAdapter or LINQ to SQL query to retrieve
information on a product
• Set data context of grid to row in DataTable or
result of LINQ query
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 14/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Demos
• Binding to an Object
• Binding to a DataTable
•
Binding to a LINQ Query• Binding to One Row of Data
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 15/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda
• Data Binding
• Using Views
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 16/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Using Views
• When you bind to a collection, WPF creates acollection view
• View manages currency (position of current item)
• Use view to navigate, sort, filter, group
• In code, use instance of CollectionView class
• To get reference to CollectionView, call
GetDefaultView method of CollectionViewSource (XAML representation of view)
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 17/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
CollectionView Class
• Navigation
• Methods: MoveCurrentToFirst, MoveCurrentToPrevious,MoveCurrentToNext, MoveCurrentToLast
• Properties: CurrentItem, CurrentPosition
• Sorting• SortDescriptions property contains collection of
SortDescription structures
• SortDescription specifies column to sort on and order
• SortDescriptions.Add adds a sorting order• SortDescriptions.Clear clears sorting
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 18/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
CollectionView Class
• Filtering
• Cast view to BindingListCollection
• Set CustomFilter property of BindingListCollection
to filter expression
• Set CustomFilter to empty string to clear filter
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 19/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
CollectionViewSource
• XAML representation of view
• Can define views in XAML rather than in code
• <Window.Resources>
• <local:NorthwindDataSet
• x:Key="northwindDataSet" />
• <CollectionViewSource
• x:Key="customersViewSource"
•
Source="{Binding Path=Customers,• Source={StaticResource
• northwindDataSet}}" />
• </Window.Resources>
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 20/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Demos
• Using a CollectionView
• Using a CollectionViewSource
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 21/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Drag and Drop Data Binding
• Drag and drop data binding supported in
Windows Forms since Visual Studio 2005
• Now supported in WPF
• Visual Studio creates CollectionViewSources in
XAML when you drag data onto a window
• One-to-many relationships automatically
handled
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 22/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Demo
• Drag and Drop Data Binding
7/31/2019 WPF: Working with Data
http://slidepdf.com/reader/full/wpf-working-with-data 23/23
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Learn More!
• This is an excerpt from a larger course. Visitwww.learnnowonline.com for the full details!
•
Learn more about WPF on SlideShare: Intro to Windows Presentation Foundation
(WPF)
WPF: Advanced Controls
WPF: Binding