Slow and steady Silverlight (15)

Source: Internet
Author: User
Tags xmlns silverlight

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.