Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Lesson 05 Data Binding in WPF
1.
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>
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.
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
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.
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.
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.
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.