WPF: Working with Data

Page 1

WPF: Working With Data

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

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Agenda • Data Binding • Using Views

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

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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


With 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 4. In Save button’s Click event handler, save changes back to database 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 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}" /> 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 text boxes 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" />

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Binding to a DataTable • Display customers in a list box and details for selected customer in text boxes in grid • Use TableAdapter or intermediary data layer to retrieve

customers 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 to

retrieve orders for selected customer

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 for selected 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 is selected • List box SelectionChanged event handler runs LINQ

to SQL query to retrieve orders for selected customer 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

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

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Agenda • Data Binding • Using Views

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Using Views • When you bind to a collection, WPF creates a collection 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) 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

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

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> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Demos • Using a CollectionView • Using a CollectionViewSource

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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Demo • Drag and Drop Data Binding

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Learn More! • This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! • Learn more about WPF on SlideShare:  Intro to Windows Presentation Foundation (WPF)  WPF: Advanced Controls  WPF: Binding

Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.