0
WPF Data Binding Explained Peter Himschoot Trainer @ U2U Microsoft Regional Director Belux www.u2u.info/Blogs/Peter
Data Binding <ul><li>The need for data binding </li></ul><ul><li>Using simple data binding </li></ul><ul><li>Converting an...
The need for Data Binding <ul><li>Imagine showing data retrieved from DB </li></ul><ul><ul><li>Move data from a column to ...
What is Data Binding Control Property Object Property One Way Two Way
Simple Data Binding <ul><ul><li>Bind one control’s property to one object’s data </li></ul></ul><ul><ul><ul><li>Control to...
Data Conversion <ul><li>Bind to another type by converting it </li></ul><ul><ul><li>Add Converter class as a resource: </l...
Building a Converter <ul><li>Implement the IValueConverter interface </li></ul>
UpdateSourceTrigger <ul><li>TextBox doesn’t change value immediately </li></ul><ul><ul><li>To do so, set UpdateSourceTrigg...
Data Validation <ul><li>Users will always make mistakes </li></ul><ul><li>Validation helps users determine these </li></ul...
Exception Validation <ul><li>Data Source can throw Exception </li></ul><ul><li>ExceptionValidationRule catches this </li><...
Custom Validation Rules <ul><li>Derive from ValidationRule </li></ul>
Using the ValidationRule <ul><li>Add it to the <ValidationRules> element </li></ul>
Custom Error Feedback <ul><li>Define a ControlTemplate resource </li></ul>
Using the ControlTemplate <ul><li>Set Validation.ErrorTemplate attached prop </li></ul><ul><li>Validation will use this fo...
Using Styles <ul><li>Even better, use a style for all textboxes </li></ul>
Using Tooltips <ul><li>User cannot see what is wrong </li></ul><ul><li>Use a tooltip to show the actual error </li></ul>
Data Binding Multiple Properties <ul><li>Avoid repeating the same source </li></ul><ul><li>Use DataContext! </li></ul>
Provide Data from Code <ul><li>May be easier to load data in codebehind </li></ul><ul><li>Can set DataContext in code </li...
Using Complex Data Binding <ul><li>Bind a control’s items to list of objects </li></ul><ul><li>Objects are displayed using...
Using Data Templates <ul><li>Display object with a DataTemplate </li></ul><ul><li>Set control’s ItemTemplate </li></ul><ul...
Selecting the DataTemplate <ul><li>By Name </li></ul><ul><li>By Type </li></ul>Type selects DataTemplate
Dynamically Selecting a Template <ul><li>Use a DataTemplateSelector </li></ul>
PersonDataTemplateSelector
Binding with DataProviders <ul><li>Use DataProvider in XAML </li></ul><ul><li>Then set ItemsSource in XAML </li></ul>Your ...
Asynchronous Data Binding <ul><li>Data Provider allows async binding </li></ul><ul><ul><li>Call method in background </li>...
Master Detail Binding <ul><li>Use ItemsControl (e.g. ListBox) as master </li></ul><ul><ul><li>Set IsSynchronizedWithCurren...
Master Detail
DisplayMemberPath <ul><li>Replaces single-field template </li></ul>
Filtering with Views <ul><li>Use a CollectionView </li></ul><ul><ul><li>CollectionViewSource in XAML </li></ul></ul><ul><l...
Sorting and Grouping <ul><li>Again use a CollectionView </li></ul>
Sorting and Grouping <ul><li>Use a CollectionViewSource </li></ul><ul><li>Define a GroupStyle </li></ul>
Binding to XML <XmlDataProvider x:Key=&quot;cars&quot; XPath=&quot;/Cars/Car&quot; Source=&quot;cars.xml&quot; /> <TextBlo...
Provide XML from Code <ul><li>Can load XML data in codebehind </li></ul><ul><ul><li>Just like any other data </li></ul></u...
Binding To DataSets <ul><li>Fill the DataSet as usual </li></ul><ul><li>Then use it as the DataContext </li></ul><ul><li>C...
Dude, where’s the DataGrid? <ul><li>DataGrid like controls not provided </li></ul><ul><li>Provided 3d party controls </li>...
Debugging Data Binding <ul><li>Binding failures generate debug messages </li></ul><ul><li>Check the Output window in Visua...
 
Upcoming SlideShare
Loading in...5
×

Dev308

492

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
492
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Dev308"

  1. 1. WPF Data Binding Explained Peter Himschoot Trainer @ U2U Microsoft Regional Director Belux www.u2u.info/Blogs/Peter
  2. 2. Data Binding <ul><li>The need for data binding </li></ul><ul><li>Using simple data binding </li></ul><ul><li>Converting and validating data </li></ul><ul><li>Using complex data binding </li></ul><ul><li>Customizing data binding with templates </li></ul><ul><li>Master-detail data binding </li></ul><ul><li>Filtering, sorting and grouping data </li></ul><ul><li>Xml, LINQ, ADO.NET data binding </li></ul><ul><li>Debugging Data Binding </li></ul>
  3. 3. The need for Data Binding <ul><li>Imagine showing data retrieved from DB </li></ul><ul><ul><li>Move data from a column to a control </li></ul></ul><ul><ul><li>Allow user to edit the data </li></ul></ul><ul><ul><li>Validate the data </li></ul></ul><ul><ul><li>Then move data from control to column </li></ul></ul><ul><ul><li>Update the column </li></ul></ul><ul><li>Data Binding automates this! </li></ul>
  4. 4. What is Data Binding Control Property Object Property One Way Two Way
  5. 5. Simple Data Binding <ul><ul><li>Bind one control’s property to one object’s data </li></ul></ul><ul><ul><ul><li>Control to control </li></ul></ul></ul><ul><ul><ul><li>Control to (data) object </li></ul></ul></ul>
  6. 6. Data Conversion <ul><li>Bind to another type by converting it </li></ul><ul><ul><li>Add Converter class as a resource: </li></ul></ul><ul><ul><li>Use Converter </li></ul></ul>
  7. 7. Building a Converter <ul><li>Implement the IValueConverter interface </li></ul>
  8. 8. UpdateSourceTrigger <ul><li>TextBox doesn’t change value immediately </li></ul><ul><ul><li>To do so, set UpdateSourceTrigger </li></ul></ul><ul><ul><li>UpdateSourceTrigger values: </li></ul></ul><ul><ul><ul><li>Default </li></ul></ul></ul><ul><ul><ul><li>LostFocus (= Default for TextBox) </li></ul></ul></ul><ul><ul><ul><li>PropertyChanged </li></ul></ul></ul><ul><ul><ul><li>Explicit </li></ul></ul></ul>
  9. 9. Data Validation <ul><li>Users will always make mistakes </li></ul><ul><li>Validation helps users determine these </li></ul><ul><li>Default DataBinding doesn’t support this </li></ul><ul><li>You need more verbose “Binding” </li></ul>
  10. 10. Exception Validation <ul><li>Data Source can throw Exception </li></ul><ul><li>ExceptionValidationRule catches this </li></ul>
  11. 11. Custom Validation Rules <ul><li>Derive from ValidationRule </li></ul>
  12. 12. Using the ValidationRule <ul><li>Add it to the <ValidationRules> element </li></ul>
  13. 13. Custom Error Feedback <ul><li>Define a ControlTemplate resource </li></ul>
  14. 14. Using the ControlTemplate <ul><li>Set Validation.ErrorTemplate attached prop </li></ul><ul><li>Validation will use this for feedback </li></ul>
  15. 15. Using Styles <ul><li>Even better, use a style for all textboxes </li></ul>
  16. 16. Using Tooltips <ul><li>User cannot see what is wrong </li></ul><ul><li>Use a tooltip to show the actual error </li></ul>
  17. 17. Data Binding Multiple Properties <ul><li>Avoid repeating the same source </li></ul><ul><li>Use DataContext! </li></ul>
  18. 18. Provide Data from Code <ul><li>May be easier to load data in codebehind </li></ul><ul><li>Can set DataContext in code </li></ul>
  19. 19. Using Complex Data Binding <ul><li>Bind a control’s items to list of objects </li></ul><ul><li>Objects are displayed using ToString() </li></ul>
  20. 20. Using Data Templates <ul><li>Display object with a DataTemplate </li></ul><ul><li>Set control’s ItemTemplate </li></ul><ul><li>DataTemplate DataSource = object </li></ul>
  21. 21. Selecting the DataTemplate <ul><li>By Name </li></ul><ul><li>By Type </li></ul>Type selects DataTemplate
  22. 22. Dynamically Selecting a Template <ul><li>Use a DataTemplateSelector </li></ul>
  23. 23. PersonDataTemplateSelector
  24. 24. Binding with DataProviders <ul><li>Use DataProvider in XAML </li></ul><ul><li>Then set ItemsSource in XAML </li></ul>Your class or Service Proxy (Optional) Method to call
  25. 25. Asynchronous Data Binding <ul><li>Data Provider allows async binding </li></ul><ul><ul><li>Call method in background </li></ul></ul>
  26. 26. Master Detail Binding <ul><li>Use ItemsControl (e.g. ListBox) as master </li></ul><ul><ul><li>Set IsSynchronizedWithCurrentItem=&quot;True&quot; </li></ul></ul><ul><li>Other bindings on same source will follow master </li></ul>
  27. 27. Master Detail
  28. 28. DisplayMemberPath <ul><li>Replaces single-field template </li></ul>
  29. 29. Filtering with Views <ul><li>Use a CollectionView </li></ul><ul><ul><li>CollectionViewSource in XAML </li></ul></ul><ul><li>Implement the Filter event </li></ul>
  30. 30. Sorting and Grouping <ul><li>Again use a CollectionView </li></ul>
  31. 31. Sorting and Grouping <ul><li>Use a CollectionViewSource </li></ul><ul><li>Define a GroupStyle </li></ul>
  32. 32. Binding to XML <XmlDataProvider x:Key=&quot;cars&quot; XPath=&quot;/Cars/Car&quot; Source=&quot;cars.xml&quot; /> <TextBlock TextContent=&quot;{Binding XPath=@Make, Source={StaticResource cars}}&quot; /> <Cars> <Car Make=&quot;Ford&quot; Model=&quot;F-150&quot;> <Image>truck.png</Image> </Car> <Car> ... </Car> </Cars> cars.xml
  33. 33. Provide XML from Code <ul><li>Can load XML data in codebehind </li></ul><ul><ul><li>Just like any other data </li></ul></ul>XmlDocument doc = new XmlDocument(); doc.LoadXml(&quot;<Foo><Bar>Hello, world</Bar></Foo>&quot;); myGrid.DataContext = doc;
  34. 34. Binding To DataSets <ul><li>Fill the DataSet as usual </li></ul><ul><li>Then use it as the DataContext </li></ul><ul><li>Controls use Path to reference </li></ul><ul><ul><li>Tables </li></ul></ul><ul><ul><li>Columns </li></ul></ul>
  35. 35. Dude, where’s the DataGrid? <ul><li>DataGrid like controls not provided </li></ul><ul><li>Provided 3d party controls </li></ul><ul><ul><ul><li>Infragistics </li></ul></ul></ul><ul><ul><ul><li>Xceed </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul>
  36. 36. Debugging Data Binding <ul><li>Binding failures generate debug messages </li></ul><ul><li>Check the Output window in Visual Studio® </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×