data binding in silverlight
DESCRIPTION
Data Binding in Silverlight presented at the WPF & Silverlight User Group in NYC 2011-02-10.TRANSCRIPT
![Page 2: Data Binding in Silverlight](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/9.jpg)
<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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051312/546af0d1af795979438b6e4d/html5/thumbnails/15.jpg)
Visual Studio ◦ Data Binding Designer
Expression Blend ◦ Data Binding Designer
◦ Design Time Data
d:DesignData
d:DataContext
d:DesignHeight and d:DesignWidth