8. DEPENDENCY PROPERTIES
• Special properties that power most of the presentation engine features
- Data Binding
- Styling
- Attached Properties
- Animation
- Property Invalidation
- Property Inheritance
- Default Values
- Sparse Storage
9. DEFINING DEPENDENCY
PROPERTIES• Inherit from DependencyObject
• Call DependencyObject.Register
• Create standard property
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(CarouselPanel),
new PropertyMetadata(200.0, new
PropertyChangedCallback(CarouselPanel.RadiusChanged)));
// optional but convenient
public double Radius
{
get { return (double)this.GetValue(RadiusProperty); }
set
{
this.SetValue(RadiusProperty, value);
}
}
10. ATTACHED PROPERTIES
• Inherit from DependencyObject
• Call DependencyProperty.RegisterAttach
• Create two static methods Getnnn/Setnnn
public static readonly DependencyProperty RowProperty =
DependencyProperty.RegisterAttached("Row", typeof(int), typeof(Grid),
new PropertyMetadata(0, new PropertyChangedCallback(OnRowChanged)));
public static void SetRow(DependencyObject obj, int value)
{
obj.SetValue(RowProperty, value);
}
public static int GetRow(DependencyObject obj )
{
return (int) obj.GetValue(RowProperty);
}
16. DATA BINDING SCENARIOS AND
BENEFITS
Connects UI to data
• When data values change, UI updates
• If two-way: when user provides input, the data is updated too
Decreases code
Enables templating
17. COMPONENTS OF UI DATA
BINDING• A Binding consists of 4 components:
1. Source
2. Source path
3. Target dependency object
4. Target dependency property
Binding Target Binding Source
Object
Property
Dependency Object
Dependency Property
Binding Object
19. BINDING TO NON-DPS
• Any public property on a CLR object will do
• Simple properties only read once by default
• Can update manually
• Updates occur only if notifications available
• INotifyPropertyChanged
21. CONVERTERS
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object
parameter, string language)
{
bool val = (bool) value ;
return ( val ?
Visibility.Visible : Visibility.Collapsed) ;
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
var vis = (Visibility)value;
return vis == Visibility.Visible;
}
}
22. DATATEMPLATE & CONTENT
MODEL
• Some controls house content:
• ContentControl: Contains a single item
• ItemsControl: Contains a collection of items
• More generally:
• Anything with a ContentPresenter
• Enables use of a DataTemplate
26. BENEFITS
• During the development process, developers and designers can work more
independently and concurrently on their components. The designers can
concentrate on the view, and if they are using Expression Blend, they can easily
generate sample data to work with, while the developers can work on the view
model and model components.
• The developers can create unit tests for the view model and the model without
using the view. The unit tests for the view model can exercise exactly the same
functionality as used by the view.
• It is easy to redesign the UI of the application without touching the code because
the view is implemented entirely in XAML. A new version of the view should work with
the existing view model.
• If there is an existing implementation of the model that encapsulates existing
business logic, it may be difficult or risky to change. In this scenario, the view model
acts as an adapter for the model classes and enables you to avoid making any
major changes to the model code.