Post on 18-Nov-2014
description
A Silverlight infrastructure to connect business model with UI Controls.
Data Binding is not about data access, data controls, data layer, data models or databases.
Not unique to Silverlight. Other technologies implement data binding: ASP.NET, Adobe Flex, WPF
The System.Windows.Data.Binding class is used to make the connection .
Binding connection is defined by 3 elements: ◦ Source of the data (CLR Object)
◦ Binding Mode (Direction of flow)
◦ Target for the data (Dependency Property)
Value Converters enhance or override binding; an extensibility point.
TextBox in XAML <TextBox x:Name=“myTextBox” />
Binding via code-behind DateTime currentTime = DateTime.Now;
Binding binding = new Binding(“TimeOfDay”);
binding.Source = currentTime;
binding.Mode = BindingMode.OneWay;
myTextBox.SetBinding(TextBox.TextProperty, binding);
Unbind a data source (code only) myTextBox.Text = “Binding Removed”;
TextBox in XAML (Binding Expression) <TextBox x:Name=“myTextBox”
Text=“{Binding TimeOfDay, Mode=OneWay}” />
---or---
<TextBox x:Name=“myTextBox”
Text=“{Binding Path=TimeOfDay, Mode=OneWay}” />
Alternate - Object Element Syntax <TextBox x:Name=“myTextBox”>
<TextBox.Text>
<Binding Path=“TimeOfDay” Mode=“OneWay” />
</TextBox.Text>
</TextBox>
ElementName
Source property
RelativeSource property ◦ Self
◦ TemplateParent
Or
If not set, the source is implicitly set via inherited DataContext.
Simple property ◦ {Binding ElementName=LayoutRoot, Path=myProperty}
Nested Property ◦ {Binding ElementName=LayoutRoot, Path=Background.Color}
Collection – set the Path to current item ◦ {Binding Path=myCollection.CurrentItem.Property}
Item in a collection - use indexer ◦ {Binding Path=MyCollection[0]}
◦ Or
◦ {Binding Path=MyCollection[Key Name]}
Binding Expression’s Mode determines the direction of data flow. OneTime - Once at Load/Parse time
OneWay (default) - Automatically update target as source changes (Read-only data model)
TwoWay - Automatically update target when source changes and update source when target changes (Updatable data model)
UpdateSourceTrigger
gives you control over when an update actually takes place. i.e. Should a source require additional (perhaps lengthy) processing before target update.
<object property=“{ Binding }” …/>
<object property=“{ Binding propertyPath } …” />
<object property=“{ Binding oneOrMoreBindingProperties } …” />
<object property=“{ Binding propertyPath, oneOrMoreBindingProperties } …” />
<object targetProperty= "{Binding sourcePropertyPath, oneOrMoreBindingProperties}" .../>
targetProperty must be a DependencyProperty
Binding Individual Elements ◦ DataContext is the default place to look for data
source. It is inherited in the logical tree. Child elements without explicit bindings inherit their DataContext from their parent. Best to use it when multiple bindings need to use the same source.
◦ Source Property is used to explicitly specify (or override) binding source and path point to property.
For automatic update, the source object must implement INotifyPropertyChanged
Use ItemsSource for Binding collections.
Automatic collection synchronization requires INotifyCollectionChanged implementation on source.
Individual collection elements still need to implement INotifyPropertyChanged.
Note: ◦ ObservableCollection<T> automatically implements
INotifyCollectionChanged.
◦ List<T> does not.
Converters are used to reformat data before presentation.
Can be used to conditionally alter and control property based on bound data.
Converter is a class that implements IValueConverter
◦ Convert() – change data to its native format to display format.
◦ ConvertBack() – reverese the process, convert display format back to original/native format.
BindingBase includes 3 properties that help eliminate the need for converters in many cases: ◦ TargetNullValue – Specifies value to use if bindings results in a null value.
◦ StringFormat – Standard .Net string formatting syntax
◦ FallBackValue – Value to use upon binding exception/failure (other than null return; i.e. invalid source attribute).
Data Templates ◦ Block of XAML used to define how bound objects
should be displayed.
◦ May be included in-line or in resources.
Validation interfaces ◦ IDataErrorInfo
Single error at a time.
Not recommended.
◦ INotifyDataErrorInfo All errors are available.
Better for Asynchronous operations.
Target property must specify ValidatesOnNotifyDataError.
Visual Studio ◦ Data Binding Designer
Expression Blend ◦ Data Binding Designer
◦ Design Time Data
d:DesignData
d:DataContext
d:DesignHeight and d:DesignWidth