WPF Data Binding Explained - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

WPF Data Binding Explained

Description:

Bind a control's items to list of objects. Objects are displayed using ... XmlDocument doc = new XmlDocument(); doc.LoadXml(' Foo Bar Hello, world /Bar /Foo ... – PowerPoint PPT presentation

Number of Views:334
Avg rating:3.0/5.0
Slides: 38
Provided by: downloadM
Category:
Tags: wpf | binding | data | explained

less

Transcript and Presenter's Notes

Title: WPF Data Binding Explained


1
WPF Data BindingExplained
  • Peter Himschoot
  • Trainer _at_ U2U
  • Microsoft Regional Director Belux
  • www.u2u.info/Blogs/Peter

2
Data Binding
  • The need for data binding
  • Using simple data binding
  • Converting and validating data
  • Using complex data binding
  • Customizing data binding with templates
  • Master-detail data binding
  • Filtering, sorting and grouping data
  • Xml, LINQ, ADO.NET data binding
  • Debugging Data Binding

3
The need for Data Binding
  • Imagine showing data retrieved from DB
  • Move data from a column to a control
  • Allow user to edit the data
  • Validate the data
  • Then move data from control to column
  • Update the column
  • Data Binding automates this!

4
What is Data Binding
Object
Property
One Way
Two Way
Control
Property
5
Simple Data Binding
  • Bind one controls property to one objects data
  • Control to control
  • Control to (data) object

6
Data Conversion
  • Bind to another type by converting it
  • Add Converter class as a resource
  • Use Converter

7
Building a Converter
  • Implement the IValueConverter interface

8
UpdateSourceTrigger
  • TextBox doesnt change value immediately
  • To do so, set UpdateSourceTrigger
  • UpdateSourceTrigger values
  • Default
  • LostFocus ( Default for TextBox)
  • PropertyChanged
  • Explicit

9
Data Validation
  • Users will always make mistakes
  • Validation helps users determine these
  • Default DataBinding doesnt support this
  • You need more verbose Binding

10
Exception Validation
  • Data Source can throw Exception
  • ExceptionValidationRule catches this

11
Custom Validation Rules
  • Derive from ValidationRule

12
Using the ValidationRule
  • Add it to the ltValidationRulesgt element

13
Custom Error Feedback
  • Define a ControlTemplate resource

14
Using the ControlTemplate
  • Set Validation.ErrorTemplate attached prop
  • Validation will use this for feedback

15
Using Styles
  • Even better, use a style for all textboxes

16
Using Tooltips
  • User cannot see what is wrong
  • Use a tooltip to show the actual error

17
Data Binding Multiple Properties
  • Avoid repeating the same source
  • Use DataContext!

18
Provide Data from Code
  • May be easier to load data in codebehind
  • Can set DataContext in code

19
Using Complex Data Binding
  • Bind a controls items to list of objects
  • Objects are displayed using ToString()

20
Using Data Templates
  • Display object with a DataTemplate
  • Set controls ItemTemplate
  • DataTemplate DataSource object

21
Selecting the DataTemplate
  • By Name
  • By Type

Type selects DataTemplate
22
Dynamically Selecting a Template
  • Use a DataTemplateSelector

23
PersonDataTemplateSelector
24
Binding with DataProviders
  • Use DataProvider in XAML
  • Then set ItemsSource in XAML

Your class or Service Proxy
(Optional) Method to call
25
Asynchronous Data Binding
  • Data Provider allows async binding
  • Call method in background

26
Master Detail Binding
  • Use ItemsControl (e.g. ListBox) as master
  • Set IsSynchronizedWithCurrentItem"True"
  • Other bindings on same source will follow master

27
Master Detail
28
DisplayMemberPath
  • Replaces single-field template

29
Filtering with Views
  • Use a CollectionView
  • CollectionViewSource in XAML
  • Implement the Filter event

30
Sorting and Grouping
  • Again use a CollectionView

31
Sorting and Grouping
  • Use a CollectionViewSource
  • Define a GroupStyle

32
Binding to XML
ltXmlDataProvider xKey"cars"
XPath"/Cars/Car" Source"cars.xml" /gt
ltTextBlock TextContent"Binding XPath_at_Make,
SourceStaticResource
cars" /gt
33
Provide XML from Code
  • Can load XML data in codebehind
  • Just like any other data

XmlDocument doc new XmlDocument() doc.LoadXml("
ltFoogtltBargtHello, worldlt/Bargtlt/Foogt") myGrid.Data
Context doc
34
Binding To DataSets
  • Fill the DataSet as usual
  • Then use it as the DataContext
  • Controls use Path to reference
  • Tables
  • Columns

35
Dude, wheres the DataGrid?
  • DataGrid like controls not provided
  • Provided 3d party controls
  • Infragistics
  • Xceed

36
Debugging Data Binding
  • Binding failures generate debug messages
  • Check the Output window in Visual Studio

37
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com