Data Binding in Silverlight

15
Boulos Dib Independent Consultant Napeague Inc. February 10, 2011

description

Data Binding in Silverlight presented at the WPF & Silverlight User Group in NYC 2011-02-10.

Transcript of Data Binding in Silverlight

Page 1: Data Binding in Silverlight

Boulos Dib Independent Consultant

Napeague Inc. February 10, 2011

Page 2: Data Binding in Silverlight

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 .

Page 3: Data Binding in Silverlight

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.

Page 4: Data Binding in Silverlight

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”;

Page 5: Data Binding in Silverlight

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>

Page 6: Data Binding in Silverlight

ElementName

Source property

RelativeSource property ◦ Self

◦ TemplateParent

Or

If not set, the source is implicitly set via inherited DataContext.

Page 7: Data Binding in Silverlight

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]}

Page 8: Data Binding in Silverlight

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.

Page 9: Data Binding in Silverlight

<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

Page 10: Data Binding in Silverlight

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

Page 11: Data Binding in Silverlight

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.

Page 12: Data Binding in Silverlight

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).

Page 13: Data Binding in Silverlight

Data Templates ◦ Block of XAML used to define how bound objects

should be displayed.

◦ May be included in-line or in resources.

Page 14: Data Binding in Silverlight

Validation interfaces ◦ IDataErrorInfo

Single error at a time.

Not recommended.

◦ INotifyDataErrorInfo All errors are available.

Better for Asynchronous operations.

Target property must specify ValidatesOnNotifyDataError.

Page 15: Data Binding in Silverlight

Visual Studio ◦ Data Binding Designer

Expression Blend ◦ Data Binding Designer

◦ Design Time Data

d:DesignData

d:DataContext

d:DesignHeight and d:DesignWidth