Binding 類別

定義

提供繫結定義的高層級存取,連接繫結目標物件的屬性 (通常為 WPF 元素) 和任何資料來源 (例如資料庫、XML 檔案或任何包含資料的物件)。Provides high-level access to the definition of a binding, which connects the properties of binding target objects (typically, WPF elements), and any data source (for example, a database, an XML file, or any object that contains data).

public ref class Binding : System::Windows::Data::BindingBase
public class Binding : System.Windows.Data.BindingBase
type Binding = class
    inherit BindingBase
Public Class Binding
Inherits BindingBase
繼承

備註

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 資料繫結在資料的展示和互動上,提供應用程式簡單而一致的方式。data binding provides a simple and consistent way for applications to present and interact with data. 資料系結可讓您同步處理兩個不同物件的屬性值。Data binding enables you to synchronize the values of the properties of two different objects.

若要建立系結, 請Binding使用類別或繼承BindingBase的其他類別之一。To establish a binding, use the Binding class or one of the other classes that inherit BindingBase. 無論您系結的物件為何, 以及資料來源的本質, 每個系結都會遵循下圖所說明的模型。Regardless of what object you are binding and the nature of your data source, each binding follows the model illustrated by the following figure.

![基本資料]系結圖表(~/add/media/databindingmostbasic.png "基本資料")系結圖表Basic data binding diagram

此圖將示範下列基本WPFWPF資料系結概念。The figure demonstrates the following fundamental WPFWPF data binding concepts.

  • 每個系結通常會有這四個元件: 系結目標物件、目標屬性、系結來源Path , 以及要使用之系結來源中的值。Each binding typically has these four components: a binding target object, a target property, a binding source, and a Path to the value in the binding source to use. 例如, 如果您想要將的內容TextBox系結至 Employee 物件的 Name 屬性, 您的目標物件TextBox是、目標屬性Text是屬性、要使用的值是 Name, 而來源物件是 Employee目標.For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

  • 目標屬性必須是相依性屬性。The target property must be a dependency property. 這也表示您無法系結欄位。This also means that you cannot bind a field. 物件的大部分UIElement屬性都是相依性屬性, 而大部分的相依性屬性 (除了唯讀以外) 預設支援資料系結。Most properties of UIElement objects are dependency properties and most dependency properties, except read-only ones, support data binding by default. (只有DependencyObject類型可以定義相依性屬性, UIElement而且所有物件DependencyObject都是衍生自)。(Only DependencyObject types can define dependency properties and all UIElement objects derive from DependencyObject.)

  • 雖然圖中未指出,但應該注意的是,繫結來源物件不限於自訂的 CLRCLR 物件。Although not specified in the figure, it should be noted that the binding source object is not restricted to being a custom CLRCLR object. WPFWPF 資料繫結支援 CLRCLR 物件和 XMLXML 格式的資料。data binding supports data in the form of CLRCLR objects and XMLXML. 為了提供一些UIElement範例, 您的系結來源可以是、任何清單物件CLRCLR 、與ADO.NETADO.NET資料或 Web 服務相關聯的物件, 或是包含您XMLXML資料的 XmlNode。To provide some examples, your binding source may be a UIElement, any list object, a CLRCLR object that is associated with ADO.NETADO.NET data or Web Services, or an XmlNode that contains your XMLXML data.

Mode使用屬性來指定資料流程的方向。Use the Mode property to specify the direction of the data flow. 若要偵測單向或雙向系結中的來源變更, 來源必須執行適當的屬性變更通知機制, 例如INotifyPropertyChangedTo detect source changes in one-way or two-way bindings, the source must implement a suitable property change notification mechanism such as INotifyPropertyChanged. 如需範例,請參閱如何:執行屬性變更通知For an example, see How to: Implement Property Change Notification. UpdateSourceTrigger屬性會指定來源更新的時間。The UpdateSourceTrigger property specifies the timing of source updates. 如需詳細資訊, 請參閱資料系結總覽中的「基本資料系結概念」。For more information, see "Basic Data Binding Concepts" in Data Binding Overview.

XAML Attribute UsageXAML Attribute Usage

<object property="{Binding  declaration}"/>  

XAML 值XAML Values

declarationdeclaration
零個或多個以逗號 (,) 分隔的屬性指派子句。Zero or more attribute-assignment clauses separated by commas (,). 如需詳細資訊, 請參閱系結標記延伸或系結宣告總覽For more information, see Binding Markup Extension or Binding Declarations Overview.

建構函式

Binding()

初始化 Binding 類別的新執行個體。Initializes a new instance of the Binding class.

Binding(String)

使用初始路徑,初始化 Binding 類別的新執行個體。Initializes a new instance of the Binding class with an initial path.

欄位

DoNothing

作為傳回值,指示繫結引擎不要執行任何動作。Used as a returned value to instruct the binding engine not to perform any action.

IndexerName

做為 PropertyNamePropertyChangedEventArgs,表示索引子屬性已變更。Used as the PropertyName of PropertyChangedEventArgs to indicate that an indexer property has changed.

SourceUpdatedEvent

識別 SourceUpdated 附加事件。Identifies the SourceUpdated attached event.

TargetUpdatedEvent

識別 TargetUpdated 附加事件。Identifies the TargetUpdated attached event.

XmlNamespaceManagerProperty

識別 XmlNamespaceManager 附加屬性。Identifies the XmlNamespaceManager attached property.

屬性

AsyncState

取得或設定傳遞至非同步資料發送器的不透明資料。Gets or sets opaque data passed to the asynchronous data dispatcher.

BindingGroupName

取得或設定這個繫結所屬之 BindingGroup 的名稱。Gets or sets the name of the BindingGroup to which this binding belongs.

(繼承來源 BindingBase)
BindsDirectlyToSource

取得或設定值,這個值表示是否評估與資料項目或 DataSourceProvider 物件相對的 PathGets or sets a value that indicates whether to evaluate the Path relative to the data item or the DataSourceProvider object.

Converter

取得或設定要使用的轉換子。Gets or sets the converter to use.

ConverterCulture

取得或設定文化特性,藉以評估轉換器。Gets or sets the culture in which to evaluate the converter.

ConverterParameter

取得或設定要傳遞至 Converter 的參數。Gets or sets the parameter to pass to the Converter.

Delay

在目標的值變更之後,取得或設定更新繫結來源之前等待的時間長度 (以毫秒為單位)。Gets or sets the amount of time, in milliseconds, to wait before updating the binding source after the value on the target changes.

(繼承來源 BindingBase)
ElementName

取得或設定要作為繫結來源物件的項目名稱。Gets or sets the name of the element to use as the binding source object.

FallbackValue

取得或設定當繫結無法傳回值時要用的值。Gets or sets the value to use when the binding is unable to return a value.

(繼承來源 BindingBase)
IsAsync

取得或設定值,指出 Binding 是否應該以非同步方式取得並設定值。Gets or sets a value that indicates whether the Binding should get and set values asynchronously.

Mode

取得或設定值,這個值表示繫結中資料流程的方向。Gets or sets a value that indicates the direction of the data flow in the binding.

NotifyOnSourceUpdated

當值從繫結目標轉送到繫結來源時,取得或設定指出是否引發 SourceUpdated 事件的值。Gets or sets a value that indicates whether to raise the SourceUpdated event when a value is transferred from the binding target to the binding source.

NotifyOnTargetUpdated

取得或設定值,指出將值從繫結來源轉送到繫結目標時,是否引發 TargetUpdated 事件。Gets or sets a value that indicates whether to raise the TargetUpdated event when a value is transferred from the binding source to the binding target.

NotifyOnValidationError

取得或設定值,指出是否要在繫結物件上引發 Error 附加事件。Gets or sets a value that indicates whether to raise the Error attached event on the bound object.

Path

取得或設定繫結程序來源屬性的路徑。Gets or sets the path to the binding source property.

RelativeSource

透過指定繫結來源對繫結目標的相對位置,取得或設定繫結來源。Gets or sets the binding source by specifying its location relative to the position of the binding target.

Source

取得或設定要作為繫結來源的物件。Gets or sets the object to use as the binding source.

StringFormat

取得或設定字串,這個字串指定繫結在其繫結值顯示成字串時的格式化方式。Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.

(繼承來源 BindingBase)
TargetNullValue

取得或設定當來源值為 null 時目標中所使用的值。Gets or sets the value that is used in the target when the value of the source is null.

(繼承來源 BindingBase)
UpdateSourceExceptionFilter

取得或設定可用來提供自訂邏輯的處理常式,以處理繫結引擎在更新繫結來源值期間所遇到的例外狀況。Gets or sets a handler you can use to provide custom logic for handling exceptions that the binding engine encounters during the update of the binding source value. 只有您已建立 ExceptionValidationRule 與繫結的關聯時才適用。This is only applicable if you have associated an ExceptionValidationRule with your binding.

UpdateSourceTrigger

取得或設定值,這個值會判斷繫結來源更新的時機。Gets or sets a value that determines the timing of binding source updates.

ValidatesOnDataErrors

取得或設定值,這個值表示是否要包含 DataErrorValidationRuleGets or sets a value that indicates whether to include the DataErrorValidationRule.

ValidatesOnExceptions

取得或設定值,這個值表示是否要包含 ExceptionValidationRuleGets or sets a value that indicates whether to include the ExceptionValidationRule.

ValidatesOnNotifyDataErrors

取得或設定值,這個值表示是否要包含 NotifyDataErrorValidationRuleGets or sets a value that indicates whether to include the NotifyDataErrorValidationRule.

ValidationRules

取得檢查使用者輸入有效性的規則集合。Gets a collection of rules that check the validity of the user input.

XPath

取得或設定 XPath 查詢,以傳回 XMLXML 繫結來源上要使用的值。Gets or sets an XPath query that returns the value on the XMLXML binding source to use.

附加屬性

XmlNamespaceManager

取得或設定 XmlNamespaceManager,用於在 XML 繫結中執行命名空間感知 XPath 查詢。Gets or sets the XmlNamespaceManager used to perform namespace-aware XPath queries in XML bindings.

方法

AddSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

加入 SourceUpdated 附加事件的處理常式。Adds a handler for the SourceUpdated attached event.

AddTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

加入 TargetUpdated 附加事件的處理常式。Adds a handler for the TargetUpdated attached event.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetXmlNamespaceManager(DependencyObject)

傳回附加至指定物件之繫結所使用的 XMLXML 命名空間管理員物件。Returns an XMLXML namespace manager object used by the binding attached to the specified object.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ProvideValue(IServiceProvider)

傳回物件,這個物件應該設定於套用這個繫結和延伸的屬性上。Returns an object that should be set on the property where this binding and extension are applied.

(繼承來源 BindingBase)
RemoveSourceUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

移除 SourceUpdated 附加事件的處理常式。Removes a handler for the SourceUpdated attached event.

RemoveTargetUpdatedHandler(DependencyObject, EventHandler<DataTransferEventArgs>)

移除 TargetUpdated 附加事件的處理常式。Removes a handler for the TargetUpdated attached event.

SetXmlNamespaceManager(DependencyObject, XmlNamespaceManager)

設定所提供項目附加的繫結所使用的命名空間管理員物件。Sets a namespace manager object used by the binding attached to the provided element.

ShouldSerializeFallbackValue()

傳回值,表示在這個類別的執行個體上,序列化處理序是否應該將 FallbackValue 屬性的有效值序列化。Returns a value that indicates whether serialization processes should serialize the effective value of the FallbackValue property on instances of this class.

(繼承來源 BindingBase)
ShouldSerializePath()

指示是否應該保存 Path 屬性。Indicates whether the Path property should be persisted.

ShouldSerializeSource()

指示是否應該保存 Source 屬性。Indicates whether the Source property should be persisted.

ShouldSerializeTargetNullValue()

傳回值,這個值表示是否應該序列化此 TargetNullValue 屬性。Returns a value that indicates whether the TargetNullValue property should be serialized.

(繼承來源 BindingBase)
ShouldSerializeValidationRules()

指示是否應該保存 ValidationRules 屬性。Indicates whether the ValidationRules property should be persisted.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

附加事件

SourceUpdated

當某個值從繫結目標傳輸至繫結來源,但只在繫結已設定為 trueNotifyOnSourceUpdated 值時發生。Occurs when a value is transferred from the binding target to the binding source, but only for bindings with the NotifyOnSourceUpdated value set to true.

TargetUpdated

發生於值從繫結來源傳送至繫結目標時,但僅針對 NotifyOnTargetUpdated 值設為 true 的繫結。Occurs when a value is transferred from the binding source to the binding target, but only for bindings with the NotifyOnTargetUpdated value set to true.

適用於

另請參閱