Title: WPF Data Binding Explained
1WPF Data BindingExplained
- Peter Himschoot
- Trainer _at_ U2U
- Microsoft Regional Director Belux
- www.u2u.info/Blogs/Peter
2Data 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
3The 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!
4What is Data Binding
Object
Property
One Way
Two Way
Control
Property
5Simple Data Binding
- Bind one controls property to one objects data
- Control to control
- Control to (data) object
6Data Conversion
- Bind to another type by converting it
- Add Converter class as a resource
- Use Converter
7Building a Converter
- Implement the IValueConverter interface
8UpdateSourceTrigger
- TextBox doesnt change value immediately
- To do so, set UpdateSourceTrigger
- UpdateSourceTrigger values
- Default
- LostFocus ( Default for TextBox)
- PropertyChanged
- Explicit
9Data Validation
- Users will always make mistakes
- Validation helps users determine these
- Default DataBinding doesnt support this
- You need more verbose Binding
10Exception Validation
- Data Source can throw Exception
- ExceptionValidationRule catches this
11Custom Validation Rules
- Derive from ValidationRule
12Using the ValidationRule
- Add it to the ltValidationRulesgt element
13Custom Error Feedback
- Define a ControlTemplate resource
14Using the ControlTemplate
- Set Validation.ErrorTemplate attached prop
- Validation will use this for feedback
15Using Styles
- Even better, use a style for all textboxes
16Using Tooltips
- User cannot see what is wrong
- Use a tooltip to show the actual error
17Data Binding Multiple Properties
- Avoid repeating the same source
- Use DataContext!
18Provide Data from Code
- May be easier to load data in codebehind
- Can set DataContext in code
19Using Complex Data Binding
- Bind a controls items to list of objects
- Objects are displayed using ToString()
20Using Data Templates
- Display object with a DataTemplate
- Set controls ItemTemplate
- DataTemplate DataSource object
21Selecting the DataTemplate
Type selects DataTemplate
22Dynamically Selecting a Template
- Use a DataTemplateSelector
23PersonDataTemplateSelector
24Binding with DataProviders
- Use DataProvider in XAML
- Then set ItemsSource in XAML
Your class or Service Proxy
(Optional) Method to call
25Asynchronous Data Binding
- Data Provider allows async binding
- Call method in background
26Master Detail Binding
- Use ItemsControl (e.g. ListBox) as master
- Set IsSynchronizedWithCurrentItem"True"
- Other bindings on same source will follow master
27Master Detail
28DisplayMemberPath
- Replaces single-field template
29Filtering with Views
- Use a CollectionView
- CollectionViewSource in XAML
- Implement the Filter event
30Sorting and Grouping
- Again use a CollectionView
31Sorting and Grouping
- Use a CollectionViewSource
- Define a GroupStyle
32Binding to XML
ltXmlDataProvider xKey"cars"
XPath"/Cars/Car" Source"cars.xml" /gt
ltTextBlock TextContent"Binding XPath_at_Make,
SourceStaticResource
cars" /gt
33Provide 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
34Binding To DataSets
- Fill the DataSet as usual
- Then use it as the DataContext
- Controls use Path to reference
- Tables
- Columns
35Dude, wheres the DataGrid?
- DataGrid like controls not provided
- Provided 3d party controls
- Infragistics
- Xceed
36Debugging Data Binding
- Binding failures generate debug messages
- Check the Output window in Visual Studio
37(No Transcript)