SlideShare a Scribd company logo
Lesson 05 Data Binding in WPF
Data binding is the mechanism by which
information contained in an object is associated
with elements in your user interface
Data binding is the process that establishes a
connection between the application UI and
business logic
 data binding is about keeping object properties
and collections of objects synchronized with one
or more controls’ views of the data
 The goal of data binding
 Save the time required to write the code to:
▪ Update controls when the data in the objects changes
▪ Update data when user edits the data in the controls
 The synchronization of the data to the controls
depends on the INotifyPropertyChanged and
INotifyCollectionChanged interfaces
 Example
 Target Object =TextBlock
 Dependency Property =Text
 Source Object = Person
 Property = Name
<Grid Name="grid" DataContext="{StaticResource Person}">
…
<TextBlockText="{Binding Path=Name}" />
</Grid>
 OneWay: Source changed Target change
 TwoWay: Source change Target change
 OneWayToSource:Target changed  Source
change
With Data Binding
Without Data Binding
 ValueConverter used to bind two properties of different types
together.
 ValueConverter converts the value from a source type to a target type
and back.
 Implementing the IValueConverter interface to create your own
ValueConverter
<Button Background="{Binding Source={StaticResource myDataSource},
Path=ColorName}">I am bound to be RED!</Button>
 We would need to create a custom converter
by implementing the IValueConverter
interface
 Collection Object must implement the
INotifyCollectionChanged interface
 Each object in collection must also implement
the INotifyPropertyChanged interface
List Data Binding
 CollectionViews are used to sort, filter, or
group the data when ItemsControl is
bounded to a data collection
 CollectionViews are classes that implement
the ICollectionView interface
 Each source collection can have multiple
views associated with it  Same data can
display in different ways
Name Type Descriptions
MoveCurrentToFirst Method Sets the first item in the view as the CurrentItem.
MoveCurrentToLast Method Sets the last item in the view as the CurrentItem.
MoveCurrentToNext Method Sets the item after the CurrentItem in the view as the CurrentItem.
MoveCurrentToPrevious Method Sets the item before the CurrentItem in the view as the CurrentItem.
CurrentItem Properties Gets the current item in the view.
Filter Properties Gets or sets a method used to determine if an item is suitable for
inclusion in the view.
Groups Properties Gets a collection of the top-level groups that is constructed based in the
GroupDescriptions property.
SortDescriptions Properties Gets a collection of SortDescription structures that describes how the
items in the collection are sorted in the view.
Using CollectionViews
Lesson 05 Data Binding in WPF
 DataTemplates allow you to define
compelling visuals for your data.
 TheWPF data binding model allows
associate ValidationRules with your Binding
object
 The ValidationRules property takes a collection
of ValidationRule objects
 There are some built-in ValidationRule in WPF
▪ ExceptionValidationRule checks for exceptions thrown during
the update of the binding source property.
▪ DataErrorValidationRule object checks for errors that are
raised by objects that implement the IDataErrorInfo
interface.
 Your own validation rule must deriving from the
ValidationRule class and implementing the
Validate method
Lesson 05 Data Binding in WPF
DataValidation
 Object Data Provider
 Derived from the DataSourceProvider
 Using object as Data Context
 Binding by Path property
 XML Data Source Provider
 Derived from the DataSourceProvider
 Using XML data as Data Context
 Binding by XPath property

More Related Content

PPTX
Cloud based testing
PPT
Slide 5 Class Diagram
PDF
Oracle E Business Suite Security Made Easy - Menus, Functions, Responsibiliti...
PDF
From MSP to MSSP using Elastic
PPTX
System Requirements
PDF
Understanding software metrics
PPTX
Cisco and AppDynamics: Redefining Application Intelligence - AppD Summit Europe
PPT
Use case diagrams
Cloud based testing
Slide 5 Class Diagram
Oracle E Business Suite Security Made Easy - Menus, Functions, Responsibiliti...
From MSP to MSSP using Elastic
System Requirements
Understanding software metrics
Cisco and AppDynamics: Redefining Application Intelligence - AppD Summit Europe
Use case diagrams

What's hot (8)

PPTX
UML and Software Modeling Tools.pptx
PPT
Chapter07 determining system requirements
PPTX
Structured Vs, Object Oriented Analysis and Design
PPT
Software Process Improvement
PDF
Insights on Knative and how it changes the serverless landscape
PDF
Software Engineering - chp5- software architecture
PPTX
Cloud Security Fundamentals Webinar
PDF
Cloud Computing Risk Management (Multi Venue)
UML and Software Modeling Tools.pptx
Chapter07 determining system requirements
Structured Vs, Object Oriented Analysis and Design
Software Process Improvement
Insights on Knative and how it changes the serverless landscape
Software Engineering - chp5- software architecture
Cloud Security Fundamentals Webinar
Cloud Computing Risk Management (Multi Venue)
Ad

Similar to Lesson 05 Data Binding in WPF (20)

PDF
Data Binding in Silverlight
PPTX
WPF - Controls &amp; Data
PPTX
The Magic of WPF & MVVM
PPTX
Introduction to XAML and its features
PDF
Data Binding and Data Grid View Classes
PPT
Dev308
PPTX
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
PPT
ADO .Net
PDF
Murach: How to transfer data from controllers
PPTX
Dependency property
PPTX
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
PPT
KnockoutJS and MVVM
PPT
Entity framework 4.0
PPTX
Entity Framework - Object Services
PPTX
Silverlight week5
PPT
the .NET Framework. It provides the claf
PDF
Flex3 data binding
PPT
PPT
Ado.net
DOC
Sap bo 4.2 course content
Data Binding in Silverlight
WPF - Controls &amp; Data
The Magic of WPF & MVVM
Introduction to XAML and its features
Data Binding and Data Grid View Classes
Dev308
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
ADO .Net
Murach: How to transfer data from controllers
Dependency property
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
KnockoutJS and MVVM
Entity framework 4.0
Entity Framework - Object Services
Silverlight week5
the .NET Framework. It provides the claf
Flex3 data binding
Ado.net
Sap bo 4.2 course content
Ad

More from Quang Nguyễn Bá (20)

PPTX
Lesson 09 Resources and Settings in WPF
PPTX
Lesson 08 Documents and Printings in WPF
PPTX
Lesson 07 Actions and Commands in WPF
PPTX
Lesson 06 Styles and Templates in WPF
PPTX
Lesson 04 WPF Controls
PPTX
Lesson 03 Layouts in WPF
PPTX
Lesson 02 Introduction to XAML
PPTX
Lesson 01 Introduction to WPF
PPTX
Business intelligence
PPTX
TDD - Test Driven Dvelopment | Test First Design
PPTX
Scrum sử dụng Team Foundation Server 2012
PPTX
Introduction to Microsoft SQL Server 2008 R2 Integration Services
PPTX
Introduction to Business Intelligence in Microsoft SQL Server 2008 R2
PPTX
Introduction to Microsoft SQL Server 2008 R2 Analysis Service
PPTX
Office 2010 Programming
PPTX
Giới thiệu WCF
PPTX
MOSS 2007 Overview
PPTX
SharePoint Programming Basic
PPTX
SharePoint 2010 Business Intelligence
PPTX
Programming SharePoint 2010 with Visual Studio 2010
Lesson 09 Resources and Settings in WPF
Lesson 08 Documents and Printings in WPF
Lesson 07 Actions and Commands in WPF
Lesson 06 Styles and Templates in WPF
Lesson 04 WPF Controls
Lesson 03 Layouts in WPF
Lesson 02 Introduction to XAML
Lesson 01 Introduction to WPF
Business intelligence
TDD - Test Driven Dvelopment | Test First Design
Scrum sử dụng Team Foundation Server 2012
Introduction to Microsoft SQL Server 2008 R2 Integration Services
Introduction to Business Intelligence in Microsoft SQL Server 2008 R2
Introduction to Microsoft SQL Server 2008 R2 Analysis Service
Office 2010 Programming
Giới thiệu WCF
MOSS 2007 Overview
SharePoint Programming Basic
SharePoint 2010 Business Intelligence
Programming SharePoint 2010 with Visual Studio 2010

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
medical staffing services at VALiNTRY
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
System and Network Administration Chapter 2
PPTX
L1 - Introduction to python Backend.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Transform Your Business with a Software ERP System
wealthsignaloriginal-com-DS-text-... (1).pdf
How to Choose the Right IT Partner for Your Business in Malaysia
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Wondershare Filmora 15 Crack With Activation Key [2025
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Operating system designcfffgfgggggggvggggggggg
medical staffing services at VALiNTRY
CHAPTER 2 - PM Management and IT Context
System and Network Administration Chapter 2
L1 - Introduction to python Backend.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
Transform Your Business with a Software ERP System

Lesson 05 Data Binding in WPF

  • 2. Data binding is the mechanism by which information contained in an object is associated with elements in your user interface Data binding is the process that establishes a connection between the application UI and business logic
  • 3.  data binding is about keeping object properties and collections of objects synchronized with one or more controls’ views of the data  The goal of data binding  Save the time required to write the code to: ▪ Update controls when the data in the objects changes ▪ Update data when user edits the data in the controls  The synchronization of the data to the controls depends on the INotifyPropertyChanged and INotifyCollectionChanged interfaces
  • 4.  Example  Target Object =TextBlock  Dependency Property =Text  Source Object = Person  Property = Name <Grid Name="grid" DataContext="{StaticResource Person}"> … <TextBlockText="{Binding Path=Name}" /> </Grid>
  • 5.  OneWay: Source changed Target change  TwoWay: Source change Target change  OneWayToSource:Target changed  Source change
  • 7.  ValueConverter used to bind two properties of different types together.  ValueConverter converts the value from a source type to a target type and back.  Implementing the IValueConverter interface to create your own ValueConverter <Button Background="{Binding Source={StaticResource myDataSource}, Path=ColorName}">I am bound to be RED!</Button>
  • 8.  We would need to create a custom converter by implementing the IValueConverter interface
  • 9.  Collection Object must implement the INotifyCollectionChanged interface  Each object in collection must also implement the INotifyPropertyChanged interface
  • 11.  CollectionViews are used to sort, filter, or group the data when ItemsControl is bounded to a data collection  CollectionViews are classes that implement the ICollectionView interface  Each source collection can have multiple views associated with it  Same data can display in different ways
  • 12. Name Type Descriptions MoveCurrentToFirst Method Sets the first item in the view as the CurrentItem. MoveCurrentToLast Method Sets the last item in the view as the CurrentItem. MoveCurrentToNext Method Sets the item after the CurrentItem in the view as the CurrentItem. MoveCurrentToPrevious Method Sets the item before the CurrentItem in the view as the CurrentItem. CurrentItem Properties Gets the current item in the view. Filter Properties Gets or sets a method used to determine if an item is suitable for inclusion in the view. Groups Properties Gets a collection of the top-level groups that is constructed based in the GroupDescriptions property. SortDescriptions Properties Gets a collection of SortDescription structures that describes how the items in the collection are sorted in the view.
  • 15.  DataTemplates allow you to define compelling visuals for your data.
  • 16.  TheWPF data binding model allows associate ValidationRules with your Binding object
  • 17.  The ValidationRules property takes a collection of ValidationRule objects  There are some built-in ValidationRule in WPF ▪ ExceptionValidationRule checks for exceptions thrown during the update of the binding source property. ▪ DataErrorValidationRule object checks for errors that are raised by objects that implement the IDataErrorInfo interface.  Your own validation rule must deriving from the ValidationRule class and implementing the Validate method
  • 20.  Object Data Provider  Derived from the DataSourceProvider  Using object as Data Context  Binding by Path property  XML Data Source Provider  Derived from the DataSourceProvider  Using XML data as Data Context  Binding by XPath property

Editor's Notes

  • #5: As illustrated by the above figure, data binding is essentially the bridge between your binding target and your binding source. The figure demonstrates the following fundamental WPF data binding concepts: Typically, each binding has these four components: a binding target object, a target property, a binding source, and a path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object. The target property must be a dependency property. Most UIElement properties are dependency properties and most dependency properties, except read-only ones, support data binding by default. (Only DependencyObject types can define dependency properties and all UIElements derive from DependencyObject.) Although not specified in the figure, it should be noted that the binding source object is not restricted to being a custom CLR object. WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data. For more information, see Binding Sources Overview.
  • #6: Not illustrated in the figure is OneTime binding, which causes the source property to initialize the target property, but subsequent changes do not propagate. This means that if the data context undergoes a change or the object in the data context changes, then the change is not reflected in the target property. This type of binding is appropriate if you are using data where either a snapshot of the current state is appropriate to use or the data is truly static. This type of binding is also useful if you want to initialize your target property with some value from a source property and the data context is not known in advance. This is essentially a simpler form of OneWay binding that provides better performance in cases where the source value does not change.
  • #10: How to Implement Collections You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes an event that should be raised whenever the underlying collection changes. WPF provides the ObservableCollection<(Of <(T>)>) class, which is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface. Note that to fully support transferring data values from source objects to targets, each object in your collection that supports bindable properties must also implement the INotifyPropertyChanged interface. For an example, see Binding to a Collection Sample. For more information, see Binding Sources Overview.
  • #17: Most applications that take user input need to have validation logic to ensure that the user has entered the expected information. The validation checks can be based on type, range, format, or other application-specific requirements.