In this article I would like to show you how to use some of the key Silverlight features:
- Dependency Properties,
- Data Bindings,
- Commands,
- Implicit styles,
- Explicit styles,
- Visual State Manager,
- Animations,
- Value Converters,
- CollectionViewSource,
- Custom Controls.
1. Dependency Properties
If you want to understand the reason and the way how DP works please visit this
link. I would like to mention differences between WPF and SL DPs. When you create DP in SL you can specify PropertyMetadata but as a result you will lack coercion and validation support which can be handled in FrameworkPropertyMetadata (WPF). These two can be useful especially if you want to create advanced controls. If you cannot imagine how coercion can be useful for you I can give an example - range IP address control.
1: public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(CelestialBodyControl), new PropertyMetadata(double.NaN));
2:
3: public double Radius
4: {
5: get { return (double)GetValue(RadiusProperty); }
6: set { SetValue(RadiusProperty, value); }
7: }
2. Data Bindings
I describe data bindings as maximum functionality and minimum code, very useful for displaying and interacting with data. Bindings are even more useful when you use Model-View-ViewModel (MVVM) pattern because in case like that data bindings create these pipelines/streams of data between presentation layer (V) and logic behind it (VM) and no additional code is needed.
Example below shows how to use data bindings directly in your view (V)
1: <local:CelestialBodyControl BodyName="{Binding Name}" Radius="{Binding Radius}" AverageDistanceFromSun="{Binding AverageDistanceFromSun}"
2: RotationPeriod="{Binding RotationPeriod}" OrbitalPeriod="{Binding OrbitalPeriod}"/>
This example shows how to use a data binding inside a style
1: <TextBlock Text="{Binding Radius, RelativeSource={RelativeSource TemplatedParent}}" Foreground="White"/>