Steady Silverlight (15)-2.0 data binding, one-way binding, bidirectional binding, inotifypropertychanged, data conversion, data validation
Introduced
Silverlight 2.0 Data binding:
Binding-Joins the properties of the binding target object with the data source
Source-bound data sources
Mode-bound data flow direction [System.Windows.Data.BindingMode enum]
Bindingmode.onetime-Once binding. To update the properties of a bound target object at a time when binding is created
Bindingmode.oneway-One-way binding (default). Changes to the data source are automatically notified to the properties of the bound target object
Bindingmode.twoway-bidirectional binding. The values of the properties of the data source or bound target object are notified when they change. Obviously, the data validation must be two-way binding.
Path-property name that needs to be bound
NotifyOnValidationError-whether the Bindingvalidationerror event is triggered when a validation error occurs. The default value is False
Validatesonexceptions-whether the binding engine will report an error when a validation error occurs. The default value is False
INotifyPropertyChanged-Notifies the client that a property value has changed
IValueConverter-Value Conversion interface that converts a value of one type to a value of another type. It provides a way to apply custom logic to bindings
Convert-forward converter. The data binding engine calls this method when the value is passed from the data source to the binding target
Convertback-Reverse converter. The data binding engine calls this method when the value is passed from the binding target to the data source
Bindingvalidationerror-This event is triggered when a validation error occurs or the last validation error is resolved
Online Demo
Http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
Example
1, Notifyproperty.xaml (demo inotifypropertychanged)
<UserControl x:Class="Silverlight20.Data.NotifyProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left">
<!--
Binding - 将绑定目标对象的属性与数据源联接起来(本例为将 Ellipse的Fill属性 与 MyColor的Brush属性 相联)
Mode - Binding 的扩展属性之一,默认为 OneWay(单向绑定),即数据源的改变会自动通知到绑定目标对象的属性
-->
<Ellipse x:Name="ellipse" Width="100" Height="50" Fill="{Binding Brush, Mode=OneWay}" MouseLeftButtonDown="ellipse_MouseLeftButtonDown" />
</StackPanel>
</UserControl>
NotifyProperty.xaml.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Net;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Animation;
Using System.Windows.Shapes;
Using System.ComponentModel;
Namespace Silverlight20.data
{
public partial class Notifyproperty:usercontrol
{
MyColor _mycolor;
Public Notifyproperty ()
{
InitializeComponent ();
This. Loaded + = new Routedeventhandler (notifyproperty_loaded);
}
void Notifyproperty_loaded (object sender, RoutedEventArgs e)
{
_mycolor = new MyColor {Brush = new SolidColorBrush (colors.red)};
Datacontext-frameworkelement data context for data binding
Bind the MyColor object to the Ellipse
Ellipse. DataContext = _mycolor;
}
private void Ellipse_mouseleftbuttondown (object sender, MouseButtonEventArgs e)
{
Modify the properties of the MyColor object after the mouse is pressed
If the MyColor implements the INotifyPropertyChanged interface, and the bindingmode of the binding target is OneWay or TwoWay, it is automatically notified to the binding target
if (_mycolor.brush.color = = colors.red)
_mycolor.brush = new SolidColorBrush (colors.green);
Else
_mycolor.brush = new SolidColorBrush (colors.red);
}
}
/**//*
* INotifyPropertyChanged-Notifies the client that a property value has changed
* INotifyPropertyChanged interface must be implemented using OneWay or TwoWay
*/
public class Mycolor:inotifypropertychanged
{
Private SolidColorBrush _brush;
Public SolidColorBrush Brush
{
get {return _brush;}
Set
{
_brush = value;
if (propertychanged!= null)
{
Trigger PropertyChanged event, event argument is property name
PropertyChanged (This, new PropertyChangedEventArgs ("Brush"));
}
}
}
Declaring a propertychanged event
public event PropertyChangedEventHandler PropertyChanged;
}
}