5. Databinding
Silverlight now supports two-way, one-way, and one-time databinding between visible controls and classes in code that represent application logic. One-way and one-time databinding are for read-only controls. Two way databinding lets the user make changes that automatically update classes in the model. You can also bind visual controls to static XAML resources, and indirectly to other visual controls, but I'll focus on making the binding in C# code here.
In XAML, you mark a control property for binding like so:
<TextBox x:Name="FontFamily" Text="{Binding FontFamily, Mode=TwoWay}"
Style="{StaticResource TextBoxStyle}" Grid.Row="0" Grid.Column="1"/>
This statement links the Text property of the FontFamily TextBox to an object's FontFamily member.
To be used for binding, an object must implement INotifyPropertyChanged:
public class Prefs : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
And then you can bind an instance of Prefs to the textbox by setting the textbox's DataContext to that instance:
FontFamily.DataContext = prefs;
We'll explore this a little further in the sample application later in the article.
6. Generics
One of the best parts of Silverlight 2 development is access to a substantial subset of the .NET 3.5 Framework and the CLR. The CLR supports generics, classes and methods that are written to work with types that aren't known until runtime. Generics allow you to write flexible classes and methods without losing the benefits of strong typing.
The most obvious application of generics is generic collection classes, where a common collection class (e.g., List) can be used to hold items of different types. The Silverlight .NET framework includes a full complement of generic collection classes that you can use in your applications.
7. ItemsControls
An ItemsControl is a UI element that displays a list of data objects. Doesn't sound like much, but the power of the ItemsControl is the flexibility that this simple mission provides. The ItemsControl can display the items of any enumerable collection, and it can display it in any fashion you like, all set up through the declarative magic of templates.
Generally, you'll use an ItemsControl by setting its ItemsSource property to a collection of objects that you manage in code. A simple way to control how each item looks is to set the ItemsControl's DisplayMemberPath to the item property to display. But for complete customization, you can instead set the ItemControl's ItemTemplate property to a DataTemplate resource. The DataTemplate functions much like a ControlTemplate, but instead of being applied to a control the DataTemplate is instead bound to each item in the collection for display:
<DataTemplate x:Key="ItemsControlDataTemplate">
<TextBlock Text="{Binding TextMember}"/>
</DataTemplate >
In addition to templating each data item, you can also supply an ItemsPanelTemplate which defines the item layout in the ItemControl.
8. Layout Management
Along with its new controls, Beta 1 provides flexible layout management derived from the layout management system of WPF. To position controls within your application, you use one of three types of layout controls:
- Canvas: Provides absolute positioning and controls don't change position with changes in container size.
- StackPanel: Stacks controls in a single column or row, vertically or horizontally, and sets control position based on container size.
- Grid: A tabular layout. Container position is specified according to row and column within the grid.
In the sample application below, we used a Grid panel as the container for the TextBoxes and other controls.
9. WebClient
Silverlight applications can consume data from REST web services using the WebClient object. In fact, if you want to get media as a stream or need to process plain text or xml data, WebClient is your best choice as it can get anything you need from a web server. The coolest thing about WebClient is that it can operate asynchronously, pulling down data in the background without blocking the UI thread. When the download is complete, WebClient fires an event to let you know that the data is available.