This document discusses data binding in Silverlight. It explains that data binding connects UI controls to business models using the Binding class. Bindings define a source, target, and binding mode. Value converters can modify bindings. Examples demonstrate binding properties in code and XAML using various binding properties and modes. Validation and data templates are also summarized.
2. 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 .
3. 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.
4. 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”;
6. ElementName
Source property
RelativeSource property
◦ Self
◦ TemplateParent
Or
If not set, the source is implicitly set via
inherited DataContext.
7. 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]}
8. 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.
10. 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
11. 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.
12. 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).
13. Data Templates
◦ Block of XAML used to define how bound objects
should be displayed.
◦ May be included in-line or in resources.
14. Validation interfaces
◦ IDataErrorInfo
Single error at a time.
Not recommended.
◦ INotifyDataErrorInfo
All errors are available.
Better for Asynchronous operations.
Target property must specify
ValidatesOnNotifyDataError.
15. Visual Studio
◦ Data Binding Designer
Expression Blend
◦ Data Binding Designer
◦ Design Time Data
d:DesignData
d:DataContext
d:DesignHeight and d:DesignWidth