Data Binding in Silverlight


Published on

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

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Data Binding in Silverlight

  1. 1. Boulos DibIndependent Consultant Napeague Inc. February 10, 2011
  2. 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. 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. 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”;
  5. 5.  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>
  6. 6.  ElementName Source property RelativeSource property ◦ Self ◦ TemplateParent Or If not set, the source is implicitly set via inherited DataContext.
  7. 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. 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.
  9. 9. <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
  10. 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. 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. 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. 13.  Data Templates ◦ Block of XAML used to define how bound objects should be displayed. ◦ May be included in-line or in resources.
  14. 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. 15.  Visual Studio ◦ Data Binding Designer Expression Blend ◦ Data Binding Designer ◦ Design Time Data  d:DesignData  d:DataContext  d:DesignHeight and d:DesignWidth