Binding 類別

定義

定義系結,此系結會連接系結目標和資料來源的屬性。

/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Binding : BindingBase
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class Binding : BindingBase
Public Class Binding
Inherits BindingBase
<Binding .../>
- or -
<dependencyobject dependencyproperty="{Binding bindingArgs}" />
繼承
Object IInspectable DependencyObject BindingBase Binding
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

下列程式碼範例示範如何在 XAML 中建立系結。 如需完整的程式代碼清單,請參閱 XAML 資料系結範例

<StackPanel Margin="5">

  <TextBlock Text="Name:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBox Text="{Binding Path=Name, Mode=TwoWay}" 
    Width="350" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBlock Text="Organization:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <!-- You can omit the 'Path=' portion of the binding expression. --> 
  <TextBox Text="{Binding Organization, Mode=TwoWay}" Width="350" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

</StackPanel>

下列範例程式碼示範如何在程式碼中建立系結。

// Create the source string.
string s = "Hello";

// Create the binding description.
Binding b = new Binding();
b.Mode = BindingMode.OneTime;
b.Source = s;

// Attach the binding to the target.
TextBlock MyText = new TextBlock();
MyText.SetBinding(TextBlock.TextProperty, b);
// Create the source string.
String^ s = ref new String(L"Hello");

// Create the binding description.
Binding^ b = ref new Binding();
b->Mode = BindingMode::OneTime;
b->Source = s;

// Attach the binding to the target.
TextBlock^ MyText = ref new TextBlock();
MyText->SetBinding(MyText->TextProperty, b);
'Create the source string 
Dim s As String = "Hello"

'Create the binding description 
Dim b As New Binding()
b.Mode = BindingMode.OneTime
b.Source = s

'Attach the binding to the target 
Dim MyText As New TextBlock()
MyText.SetBinding(TextBlock.TextProperty, b)

備註

{Binding} 標記延伸可讓您在 XAML 中將 Binding 值指定為單一屬性字串,包括設定系結屬性,例如PathSource。 如需資料系結概念的詳細資訊,請參閱 深入資料系結

Binding 類別可能會被視為 {Binding} 標記延伸的程式碼後置曝光。 如果系結已套用至目標 (當 XAML 載入) 時發生,則您無法設定 Binding 物件的讀寫屬性,以變更系結在執行時間的行為。 任何 XAML 定義的系結都應該視為不可變。 但是,您可以建立新的 Binding 物件、設定其屬性,並使用 FrameworkElement.SetBinding在特定 UI 元素目標上建立新的系結。 如需詳細資訊,請參閱 在程式碼中建立系結

Binding 物件會將 FrameworkElement 的相依性屬性直接連接到資料物件,以便將資料物件的更新自動傳播至使用資料系結的屬性。 Binding 類別會定義系結的屬性。 每個系結都必須有目標專案、目標屬性和資料來源,但如果您未指定這些值,則預設會提供某些值。

若要系結至資料物件上的屬性或子屬性,請設定 Binding 物件的 Path 屬性。 如需如何在程式碼或 XAML 中設定 Path 的詳細資訊,請參閱 屬性路徑語法{Binding} 標記延伸

您可以將 Binding 類別的實例套用至多個目標。 不過,當您將 Binding 物件的屬性值附加至目標元素之後,就無法修改它。

注意

呼叫 FrameworkElement.SetBinding 方法並傳入新的 Binding 物件不一定會移除現有的系結。 您應該改用 DependencyObject.ClearValue 方法。

如需可採用系結之屬性的 XAML 屬性使用方式的詳細資訊,或可以設定為數據系結值的詳細資訊,請參閱 {Binding} 標記延伸

做為資料繫結目標的屬性必須是相依性屬性。 如需詳細資訊,請參閱相依性屬性概觀

建構函式

Binding()

初始化 Binding 類別的新實例。

屬性

Converter

取得或設定系結引擎所呼叫的轉換子物件,以在來源與目標之間傳遞資料時修改資料,反之亦然。

ConverterLanguage

取得或設定值,這個值會命名要傳遞至 Converter 屬性所指定之任何轉換器的語言。

ConverterParameter

取得或設定可在 Converter 邏輯中使用的參數。

Dispatcher

取得與此物件相關聯的 CoreDispatcherCoreDispatcher代表可以存取 UI 執行緒上DependencyObject的功能,即使程式碼是由非 UI 執行緒起始也一樣。

(繼承來源 DependencyObject)
ElementName

取得或設定做為 Binding之系結來源的專案名稱。

FallbackValue

取得或設定當繫結無法傳回值時要用的值。

Mode

取得或設定值,這個值表示繫結中資料流程的方向。

Path

取得或設定繫結程序來源屬性的路徑。

RelativeSource

透過指定繫結來源對繫結目標的相對位置,取得或設定繫結來源。 這最常用於 XAML 控制項範本內的繫結中。

Source

取得或設定系結的資料來源。

TargetNullValue

取得或設定當來源值為 Null時,目標中使用的值。

UpdateSourceTrigger

取得或設定值,這個值會決定雙向系結之系結來源更新的時間。

方法

ClearValue(DependencyProperty)

清除相依性屬性的本機值。

(繼承來源 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

傳回針對相依性屬性所建立的任何基底值,如果動畫未使用中,則會套用。

(繼承來源 DependencyObject)
GetValue(DependencyProperty)

DependencyObject傳回相依性屬性的目前有效值。

(繼承來源 DependencyObject)
ReadLocalValue(DependencyProperty)

如果已設定本機值,則傳回相依性屬性的本機值。

(繼承來源 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

註冊通知函式,以接聽此DependencyObject實例上特定DependencyProperty的變更。

(繼承來源 DependencyObject)
SetValue(DependencyProperty, Object)

DependencyObject上設定相依性屬性的本機值。

(繼承來源 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消先前透過呼叫 RegisterPropertyChangedCallback註冊的變更通知。

(繼承來源 DependencyObject)

適用於

另請參閱