SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
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

What's hot (6)

DOC
Ruby On Rails
iradarji
 
PPTX
Koštano tkivo i okoštavanje
Vesna Danilovic
 
PDF
Online Testing Learning to Rank with Solr Interleaving
Sease
 
PPT
Urogenitalni trakt
dr Šarac
 
PPTX
Elasticsearch 한글 형태소 분석기 Nori 노리
종민 김
 
PPT
Anatomija pokreta pojedinih segmenata tela
Anatomija dr Šarac
 
Ruby On Rails
iradarji
 
Koštano tkivo i okoštavanje
Vesna Danilovic
 
Online Testing Learning to Rank with Solr Interleaving
Sease
 
Urogenitalni trakt
dr Šarac
 
Elasticsearch 한글 형태소 분석기 Nori 노리
종민 김
 
Anatomija pokreta pojedinih segmenata tela
Anatomija dr Šarac
 

Similar to Lesson 05 Data Binding in WPF (20)

PDF
Data Binding in Silverlight
Boulos Dib
 
PPT
Dev308
guest2130e
 
PPTX
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
ABTO Software
 
PPTX
Introduction to XAML and its features
Abhishek Sur
 
PPTX
Data Bondage in WPF
Bruce Johnson
 
PPTX
The Magic of WPF & MVVM
Abhishek Sur
 
PPTX
WPF - Controls &amp; Data
Sharada Gururaj
 
PPT
Simple Data Binding
Doncho Minkov
 
PPTX
Silverlight week5
iedotnetug
 
PDF
WPF DATA BINDING CHEATSHEET V1.1
Vikas Pandey
 
PPT
Complex Data Binding
Doncho Minkov
 
PPTX
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
ABTO Software
 
KEY
WPF Binding
LearnNowOnline
 
PDF
Data Binding and Data Grid View Classes
Arvind Krishnaa
 
PPTX
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
iFour Technolab Pvt. Ltd.
 
PPTX
WPF with MVVM: From the Trenches
Brent Edwards
 
KEY
What's new in Silverlight 5
LearnNowOnline
 
PPTX
My XML is Alive! An Intro to XAML
Jeremy Likness
 
PDF
Tool Development 10 - MVVM, Tool Chains
Nick Pruehs
 
PPT
WPF and Databases
Doncho Minkov
 
Data Binding in Silverlight
Boulos Dib
 
Dev308
guest2130e
 
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
ABTO Software
 
Introduction to XAML and its features
Abhishek Sur
 
Data Bondage in WPF
Bruce Johnson
 
The Magic of WPF & MVVM
Abhishek Sur
 
WPF - Controls &amp; Data
Sharada Gururaj
 
Simple Data Binding
Doncho Minkov
 
Silverlight week5
iedotnetug
 
WPF DATA BINDING CHEATSHEET V1.1
Vikas Pandey
 
Complex Data Binding
Doncho Minkov
 
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
ABTO Software
 
WPF Binding
LearnNowOnline
 
Data Binding and Data Grid View Classes
Arvind Krishnaa
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
iFour Technolab Pvt. Ltd.
 
WPF with MVVM: From the Trenches
Brent Edwards
 
What's new in Silverlight 5
LearnNowOnline
 
My XML is Alive! An Intro to XAML
Jeremy Likness
 
Tool Development 10 - MVVM, Tool Chains
Nick Pruehs
 
WPF and Databases
Doncho Minkov
 
Ad

More from Quang Nguyễn Bá (20)

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

Recently uploaded (20)

PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Human Resources Information System (HRIS)
Amity University, Patna
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 

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.