數據綁定是一種簡單方式來顯示數據,UI元素與數據對象之間的連接或綁定是允許數據在兩者之間 流動的。另外建立了綁定且數據發生變化時,相應的UI元素會自動顯示變化。
如何將UI元素與 數據進行綁定

從上面圖可以知道,每個綁定必須指定一個源和一個目標。
其中源對象可以是任何CLR對象,包括目標元素自身和其他UI元素。目標可以是 FrameworkElement的任何DependencyProperty(依賴屬性)。
數據綁定引擎從Binding對象獲取 以下內容:
1)源對象和目標對象;
2)數據流的方向;
3)值轉換器;
例如:使用C#代碼和XAML將TextBox的Foreground屬性進行綁定。
XAML代碼:
<TextBoxx:Name="MyTextBox"Text="Text"Foreground="{Binding Brush1,
Mode=OneWay}"/>
C#代碼:
MyColors textcolor = new MyColors ();
textcolor.Brush1 = new SolidColorBrush (Colors.Red);
MyTextBox.DataContext = textcolor;
綁定是使用 {Binding…}語法在XAML中創建的。源是通過設置TextBox的DataContent屬性在代碼中設置的。
另外數據是會被繼承的。若我們在某個父元素上設置數據上下文,那麼其子元素將使用同一數據。我們 可以通過設置Binding.Path屬性綁定到源對象的某個屬性。
數據綁定的方向
每一個綁 定都包含一個Mode屬性,用於確定數據流動的方式和時間。
三種類型的綁定:
1) OneTime:綁定會在創建時使用源數據更新目標。
2)OneWay:綁定會在創建時以及數據發生更 改時使用源數據更新目標(默認模式)。
3)TwoWay:綁定會在目標和源中的任一個發生更改時 同時更新目標和源。
數據更改通知
當源數據對象進行了更改,如何將新的源數據對象 傳遞給目標對象呢?解決辦法是源數據對象繼承INotifyPropertyChanged 接口。因為 INotifyPropertyChanged 接口提供了PropertyChanged 事件,該事件會告訴數據綁定引擎,源對象已 經改變,方便更改目標值。
例如:
// Create a class that implements
INotifyPropertyChanged.
public class MyColors : INotifyPropertyChanged
{
private SolidColorBrush _Brush1;
// Declare the PropertyChanged event.
public event PropertyChangedEventHandler PropertyChanged;
// Create the property that will be the source of the binding.
public SolidColorBrush Brush1
{
get { return _Brush1; }
set
{
_Brush1 = value;
// Call NotifyPropertyChanged when the source property
// is updated.
NotifyPropertyChanged("Brush1");
}
}
// NotifyPropertyChanged will raise the PropertyChanged event,
// passing the source property that is being updated.
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
示例代碼中MyColors類繼承了INotifyPropertyChanged 接口,當Brush1屬性更改時將觸發 PropertyChanged事件通知目標對象以及更改。
數據轉換
當我們存儲的數據在UI界面顯 示的時候,對用戶來講不友好時,可以將數據進行相應的轉換進行顯示。這時我們就需要一個數據的轉 換器了。
我們可以在任意的綁定上設置轉換器,並且自定義轉換器類必須繼承實現 IValueConverter 接口。
例如:
// Custom class implements the IValueConverter
interface.
public class DateToStringConverter : IValueConverter
{
#region IValueConverter Members
// Define the Convert method to change a DateTime object to
// a month string.
public object Convert(object value, Type targetType,
object parameter, string language)
{
// value is the data from the source object.
DateTime thisdate = (DateTime)value;
int monthnum = thisdate.Month;
string month;
switch (monthnum)
{
case 1:
month = "January";
break;
case 2:
month = "February";
break;
default:
month = "Month not found";
break;
}
// Return the value to pass to the target.
return month;
}
// ConvertBack is not implemented for a OneWay binding.
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
#endregion
}
當從源對象傳遞數據時,綁定引擎會調用Convert 並將返回的數據傳遞回目標。
當 從目標傳遞數據時,綁定引擎會調用ConvertBack 並將返回的數據傳遞回源。
XAML代碼中設置 Converter:
<UserControl.Resources>
<local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
...
<TextBlock Grid.Column="0"
Text="{Binding Month, Converter={StaticResource Converter1}}"/>
轉換器中還有 兩個可選參數:ConverterLanguage(該參數允許指定在轉換中使用的語言)和 ConverterParameter( 該參數允許為轉換邏輯傳遞一個參數)。
注意:若在數據轉換中存在錯誤,最好不要拋出異常 ,而是返回DependencyProperty.UnsetValue,它將停止數據傳輸。
支持的綁定方案

注:該表格根據MSDN提供的文檔進行相應的整理。