90 likes | 162 Views
Data Binding in Silverlight. Mahender Senior Software Engineer United Health Group. Mahender Sarangam.
E N D
Data Binding in Silverlight Mahender Senior Software Engineer United Health Group
Mahender Sarangam Having close to 5 years of experience. Working as a Senior Software Engineer in United Health Group. Good Knowledge on C#, ASP.NET, Silverlight, WPF ,SQL Server, Team Foundation Server(TFS) and SharePoint Technology. MCTS Certified in Web Technologies.
Data Binding • Data binding is a mechanism for moving the data from objects into UI controls. • Data binding is a key in Silverlight that allows data shown on UI and then process changes. • Data binding is a process that tells Silverlight to extract a property value from a source object and use it to set a property in a target object. Here Target Object which inherit from Dependency Object and its property must be a Dependency property. • Data Binding works with Public properties of Source object. It does not work for fields.
Data Binding • We use Binding expression for binding Source property to Target property. Here we don’t mention source object which contain data. Generally in WPF/Silverlight, control uses Data context as Source Object or try to use source property of Binding expression. <UserControl.Resources> <local:Product x:Key="resourceProduct" ModelNumber="AEFS100" ModelName="Portable Defibrillator" UnitCost="77" Description="Analyzes the electrical activity of a person's heart and applies an electric shock if necessary."> </local:Product> </UserControl.Resources> <TextBox Text="{Binding ModelNumber, Source={StaticResourceresourceProduct} }“></TextBox>
INotify Property Changed • The INotifyPropertyChanged interface is a key part of the data binding infrastructure available in Silverlight. It contains a single event named PropertyChanged that is used to notify objects when a particular property value changes.
Data Context • Data Context: The Data Context refers to a source of data that can be bound to a target. The Data Context often is set to an instance of an entity • Data Context is inherited to child elements and this makes it ideal for scenarios where you want to bind multiple elements to a single data source. • Data Context is a property of Framework Element • We use Binding expression for binding Source property to Target property
Target Null Value and Fall Back values • Target Null values are default values i.e. when bound data is NULL or there may be cases where value bounded is NULL. • For example: <TextBox Text="{Binding ModelName, TargetNullValue='[No Model Name Set]' }"></TextBox> • There might be cases , When Binding Properties to Target control ,binding throws exception or any binding operation failure , Fall back value provide a fall back value when binding is unable to return value • <TextBox Text="{Binding ModelName, FallBackvalue='[No Model Name Set]' }"></TextBox>. • String Format : Allows formatting of the bound value using standard formatting expressions. • <TextBlock x:Name="CurrencyExample" Text="{Binding Cost, StringFormat=C}"></TextBlock> • <TextBlock x:Name="BirthDateValueTextBlock" Text="{Binding BirthDate, StringFormat=MM/dd/yyyy}"></TextBlock>
Data Flow • Each Binding expression has Mode property which determines how and when the data flows. • There are 3 types of Data binding • One Time Binding : Target property is set initially with the source data when the binding is created • One way Binding : Update targets whenever source data changes. This is default • Two Way Binding : The target property is updated when the source property changes, and the source Property is updated when the target property changes. • UpdateSourceTrigger : Silverlight supports only two values for Update SourceTrigger: Default and Explicit. It isn’t possible to choose PropertyChanged • ValidateonErrors and validate on exceptions