穩紮穩打Silverlight(15) - 2.0資料之一次綁定,單向綁定,雙向繫結,INotifyPropertyChanged,資料轉換,資料驗證
介紹
Silverlight 2.0 資料繫結:
Binding - 將繫結目標對象的屬性與資料來源聯結起來
Source - 繫結資料源
Mode - 繫結資料流的方向 [System.Windows.Data.BindingMode枚舉]
BindingMode.OneTime - 一次綁定。建立綁定時一次性地更新繫結目標對象的屬性
BindingMode.OneWay - 單向綁定(預設值)。資料來源的改變會自動通知到繫結目標對象的屬性
BindingMode.TwoWay - 雙向繫結。資料來源或繫結目標對象的屬性的值發生改變時會互相通知。顯然,做資料驗證的話一定要是雙向繫結
Path - 需要綁定的屬性名稱
NotifyOnValidationError - 產生驗證錯誤時是否觸發 BindingValidationError 事件。預設值為 false
ValidatesOnExceptions - 產生驗證錯誤時綁定引擎是否要報告錯誤。預設值為 false
INotifyPropertyChanged - 向用戶端發出某一屬性值已更改的通知
IValueConverter - 值轉換介面,將一個類型的值轉換為另一個類型的值。它提供了一種將自訂邏輯應用程式於綁定的方式
Convert - 正向轉換器。將值從資料來源傳給繫結目標時,資料繫結引擎會調用此方法
ConvertBack - 反向轉換器。將值從繫結目標傳給資料來源時,資料繫結引擎會調用此方法
BindingValidationError - 出現驗證錯誤或解決上次驗證錯誤則觸發此事件
線上DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
樣本
1、NotifyProperty.xaml(示範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 做資料繫結時的資料內容
// 將 MyColor 對象綁定到 Ellipse
ellipse.DataContext = _myColor;
}
private void ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 滑鼠按下後修改 MyColor 對象的屬性
// 如果MyColor實現了INotifyPropertyChanged介面,並且繫結目標的BindingMode為OneWay或者TwoWay的話則會自動通知到繫結目標
if (_myColor.Brush.Color == Colors.Red)
_myColor.Brush = new SolidColorBrush(Colors.Green);
else
_myColor.Brush = new SolidColorBrush(Colors.Red);
}
}
/**//*
* INotifyPropertyChanged - 向用戶端發出某一屬性值已更改的通知
* 使用 OneWay 或者 TwoWay 的話必須要實現 INotifyPropertyChanged 介面
*/
public class MyColor : INotifyPropertyChanged
{
private SolidColorBrush _brush;
public SolidColorBrush Brush
{
get { return _brush; }
set
{
_brush = value;
if (PropertyChanged != null)
{
// 觸發 PropertyChanged 事件,事件參數為屬性名稱
PropertyChanged(this, new PropertyChangedEventArgs("Brush"));
}
}
}
// 聲明一個 PropertyChanged 事件
public event PropertyChangedEventHandler PropertyChanged;
}
}