|
|
|
Interaction with data in any technology is very important. Here providing how can we access data using Silverlight. Accessing data using LINQ and Data Binding approaches in Silverlight.
LINQ
• LINQ – A new feature in VS 2008 and .NET framework 3.5 • LINQ – Language Integrated Query, a new query language that targets data stored in objects and collections. • Extended to query relational data stored in databases, XML data, and other data sources, however data should be stored as Objects • Supports Type Checking and Intellisense
Data Binding
Data binding is a connection between the User Interface and a business object or other data provider. The User Interface object is called the target, the provider of the data is called the source.
Data Binding Modes:
OneTime: Updates the target property when the binding is created. Meaning that any changes to the data source after the initial binding will not be reflected in the user interface.
OneWay: Updates the target property when the binding is created. Changes to the source object can also propagate to the target. TwoWay: Updates either the target or the source object when either changes. When the binding is created, the target property is updated from the source. Data template is a chunk of XAML markup that defines how a bound data object should be displayed.
Two types of controls support data templates: • Content controls support data templates through the ContentTemplate property. • List controls support data templates through the ItemTemplate property.
Define Data Template:
DataTemplate x:Key="dtAddress" Grid Grid.RowDefinitions RowDefinition Height="Auto"/ /Grid.RowDefinitions Grid.ColumnDefinitions ColumnDefinition Width="Auto" / /Grid.ColumnDefinitions< TextBlock x:Name="tblkStreet" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding Street}" Foreground="White" FontSize="12"/ /Grid /DataTemplate
Using Data Template in Content or List controls:
ContentControl ContentTemplate="{StaticResource dtAddress}" / ListBox ItemTemplate="{StaticResource dtAddress}" /
Change Notifications Notifying the modifications in the source data object changes to the bound target controls automatically. Implementation of the PropertyChanged event:
public class Product : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } } Fire the PropertyChanged event in all property setters: private double unitCost; public double UnitCost { get { return unitCost; } set { unitCost = value; OnPropertyChanged(new PropertyChangedEventArgs("UnitCost")); } }
Data Conversion • Silverlight data conversion – Using value converter class. Value converters are an extremely useful piece of the Silverlight data binding puzzle. 1.To format data to a string representation 2.To create a specific type of Silverlight object 3.To conditionally alter a property in an element based on the bound data
The following table displays the various valid XAML syntax for the Binding markup extension.
Syntax Description {Binding} This signals data binding. The mode of operation is OneWay. This is most commonly used with item templates for controls such as the ListBox. {Binding path} This signals data binding and specifies which property will supply the data. The path takes the form of object properties separated by dots, allowing you to drill down into an object. {Binding properties} This signals data binding but provides the ability to set data binding configuration properties using a name=value syntax. {Binding path, properties} This combines the previous two formats, allowing you to specify which object property supplies the data and also configure the data binding.
Conclusion: With this we come to know how can we access data in Silvlight using LINQ and DataBinding.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|