繫結標記延伸Binding Markup Extension

將屬性值延遲為數據系結的值,建立中繼運算式物件,並在執行時間解讀套用至元素及其系結的資料內容。Defers a property value to be a data-bound value, creating an intermediate expression object and interpreting the data context that applies to the element and its binding at run time.

系結運算式使用方式Binding Expression Usage

<object property="{Binding}" .../>  
-or-  
<object property="{Binding  bindProp1=value1[, bindPropN=valueN]*}" ...  
/>  
-or-  
<object property="{Binding path}" .../>  
-or  
<object property="{Binding path[, bindPropN=valueN]*}" .../>  

語法注意事項Syntax Notes

在這些語法中,[]* 不是常值。In these syntaxes, the [] and * are not literals. 它們屬於標記法的一部分,表示可以使用零或多個bindProp=組,並在兩者之間加上 , 分隔符號和前面的bindProp=配對。They are part of a notation to indicate that zero or more bindProp=value pairs can be used, with a , separator between them and preceding bindProp=value pairs.

「可使用系結延伸模組設定的系結屬性」區段中所列的任何屬性,都可以改用 Binding 物件專案的屬性來設定。Any of the properties listed in the "Binding Properties That Can Be Set with the Binding Extension" section could instead be set using attributes of a Binding object element. 不過,這不是真正 Binding的標記延伸使用方式,只是設定 CLR Binding 類別屬性之屬性的一般 XAML 處理。However, that is not truly the markup extension usage of Binding, it is just the general XAML processing of attributes that set properties of the CLR Binding class. 換句話說,<Binding bindProp1="value1"[ bindPropN=""]*/>Binding 物件專案使用方式(而非 Binding 運算式使用方式)之屬性的對等語法。In other words, <Binding bindProp1="value1"[ bindPropN="valueN"]*/> is an equivalent syntax for attributes of Binding object element usage instead of a Binding expression usage. 若要瞭解 Binding的特定屬性之 XAML 屬性用法,請參閱 .NET Framework 類別庫中 Binding 相關屬性的「XAML 屬性使用方式」一節。To learn about the XAML attribute usage of specific properties of Binding, see the "XAML Attribute Usage" section of the relevant property of Binding in the .NET Framework Class Library.

XAML 值XAML Values

bindProp1, bindPropN 要設定的 BindingBindingBase 屬性的名稱。The name of the Binding or BindingBase property to set. 並非所有 Binding 屬性都可以使用 Binding 延伸模組來設定,而且有些屬性只能使用進一步的嵌套標記延伸,在 Binding 運算式內設定。Not all Binding properties can be set with the Binding extension, and some properties are settable within a Binding expression only by using further nested markup extensions. 請參閱「可使用系結延伸模組設定的系結屬性」一節。See "Binding Properties That Can Be Set with the Binding Extension" section.
value1, valueN 要將屬性設定為的值。The value to set the property to. 屬性值的處理最終是特定于所設定之特定 Binding 屬性的型別和邏輯。The handling of the attribute value is ultimately specific to the type and logic of the specific Binding property being set.
path 設定隱含 Binding.Path 屬性的路徑字串。The path string that sets the implicit Binding.Path property. 另請參閱PROPERTYPATH XAML 語法See also PropertyPath XAML Syntax.

不合格的 {Binding}Unqualified {Binding}

「系結運算式使用方式」中所顯示的 {Binding} 用法,會建立具有預設值的 Binding 物件,其中包含 null的初始 Binding.PathThe {Binding} usage shown in "Binding Expression Usage" creates a Binding object with default values, which includes an initial Binding.Path of null. 這在許多情況下仍然很有用,因為建立的 Binding 可能依賴索引鍵資料系結屬性,例如在執行時間資料內容中設定的 Binding.PathBinding.SourceThis is still useful in many scenarios, because the created Binding might be relying on key data binding properties such as Binding.Path and Binding.Source being set in the run-time data context. 如需資料內容概念的詳細資訊,請參閱資料系結。For more information on the concept of data context, see Data Binding.

隱含路徑Implicit Path

Binding 標記延伸使用 Binding.Path 做為概念「預設屬性」,其中 Path= 不需要出現在運算式中。The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression. 如果您指定具有隱含路徑的 Binding 運算式,則隱含路徑必須先出現在運算式中,在任何其他 bindProp=value 組之前,其中 Binding 屬性是以名稱指定。If you specify a Binding expression with an implicit path, the implicit path must appear first in the expression, prior to any other bindProp=value pairs where the Binding property is specified by name. 例如: {Binding PathString},其中 PathString 是在標記延伸使用方式所建立的 Binding 中,評估為 Binding.Path 值的字串。For example: {Binding PathString}, where PathString is a string that is evaluated to be the value of Binding.Path in the Binding created by the markup extension usage. 您可以在逗號分隔符號後面附加具有其他命名屬性的隱含路徑,例如 {Binding LastName, Mode=TwoWay}You can append an implicit path with other named properties after the comma separator, for example, {Binding LastName, Mode=TwoWay}.

可以使用系結延伸模組設定的系結屬性Binding Properties That Can Be Set with the Binding Extension

本主題所顯示的語法會使用泛型 bindProp=value 近似值,因為有許多 BindingBaseBinding 的讀取/寫入屬性可以透過 Binding 標記延伸/運算式語法來設定。The syntax shown in this topic uses the generic bindProp=value approximation, because there are many read/write properties of BindingBase or Binding that can be set through the Binding markup extension / expression syntax. 它們可以依任何順序設定,但隱含 Binding.Path除外。They can be set in any order, with the exception of an implicit Binding.Path. (您可以選擇明確指定 Path=,在此情況下,可以依任何順序設定)。(You do have the option to explicitly specify Path=, in which case it can be set in any order). 基本上,您可以使用 bindProp=value 組(以逗號分隔),在下列清單中設定零個或多個屬性。Basically, you can set zero or more of the properties in the list below, using bindProp=value pairs separated by commas.

其中有幾個屬性值需要的物件類型不支援 XAML 中文字語法的原生類型轉換,因此需要標記延伸才能設定為屬性值。Several of these property values require object types that do not support a native type conversion from a text syntax in XAML, and thus require markup extensions in order to be set as an attribute value. 如需詳細資訊,請查看每個屬性的 .NET Framework 類別庫中的 XAML 屬性使用方式一節。您用於具有或不含進一步標記延伸使用方式之 XAML 屬性語法的字串,基本上與您在 Binding 運算式中指定的值相同,唯一的例外是您不會將引號括在每個 bindProp=value``Binding 運算式。Check the XAML Attribute Usage section in the .NET Framework Class Library for each property for more information; the string you use for XAML attribute syntax with or without further markup extension usage is basically the same as the value you specify in a Binding expression, with the exception that you do not place quotation marks around each bindProp=value in the Binding expression.

以下是無法使用 Binding 標記延伸/{Binding} 運算式形式設定的 Binding 屬性。The following are properties of Binding that cannot be set using the Binding markup extension/{Binding} expression form.

  • UpdateSourceExceptionFilter:此屬性需要回呼執行的參考。UpdateSourceExceptionFilter: this property expects a reference to a callback implementation. 不能在 XAML 語法中參考事件處理常式以外的回呼/方法。Callbacks/methods other than event handlers cannot be referenced in XAML syntax.

  • ValidationRules:屬性會接受 ValidationRule 物件的泛型集合。ValidationRules: the property takes a generic collection of ValidationRule objects. 這可能會表示為 Binding 物件專案中的 property 專案,但在 Binding 運算式中沒有任何可供使用的屬性剖析技術。This could be expressed as a property element in a Binding object element, but has no readily available attribute-parsing technique for usage in a Binding expression. 如需 ValidationRules,請參閱參考主題。See reference topic for ValidationRules.

  • XmlNamespaceManager

備註Remarks

重要

就相依性屬性優先順序而言,Binding 運算式就相當於本機設定的值。In terms of dependency property precedence, a Binding expression is equivalent to a locally set value. 如果您為先前有 Binding 運算式的屬性設定本機值,Binding 就會完全移除。If you set a local value for a property that previously had a Binding expression, the Binding is completely removed. 如需詳細資訊,請參閱相依性屬性值優先順序For details, see Dependency Property Value Precedence.

本主題未涵蓋描述基本層級的資料系結。Describing data binding at a basic level is not covered in this topic. 請參閱資料系結總覽See Data Binding Overview.

注意

MultiBindingPriorityBinding 不支援 XAMLXAML 擴充功能語法。MultiBinding and PriorityBinding do not support a XAMLXAML extension syntax. 您會改為使用屬性元素。You would instead use property elements. 請參閱 MultiBindingPriorityBinding的參考主題。See reference topics for MultiBinding and PriorityBinding.

XAML 的布林值不區分大小寫。Boolean values for XAML are case insensitive. 例如,您可以指定 {Binding NotifyOnValidationError=true}{Binding NotifyOnValidationError=True}For example you could specify either {Binding NotifyOnValidationError=true} or {Binding NotifyOnValidationError=True}.

牽涉到資料驗證的系結通常是由明確的 Binding 專案指定,而非 {Binding ...} 運算式,而且在運算式中設定 ValidatesOnDataErrorsValidatesOnExceptions 並不常見。Bindings that involve data validation are typically specified by an explicit Binding element rather than as a {Binding ...} expression, and setting ValidatesOnDataErrors or ValidatesOnExceptions in an expression is uncommon. 這是因為隨附的屬性 ValidationRules 無法在運算式表單中輕鬆設定。This is because the companion property ValidationRules cannot be readily set in the expression form. 如需詳細資訊,請參閱執行系結驗證For more information, see Implement Binding Validation.

Binding 是一種標記延伸。Binding is a markup extension. 標記延伸通常會在需要將屬性值換成常值或處理常式名稱以外,而且需求比特定類型或屬性上的類型轉換器更通用。Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than type converters attributed on certain types or properties. XAML 中的所有標記延伸都會使用其屬性語法中的 {} 字元,這是 XAML 處理器辨識標記延伸必須處理字串內容的慣例。All markup extensions in XAML use the { and } characters in their attribute syntax, which is the convention by which a XAML processor recognizes that a markup extension must process the string contents. 如需詳細資訊,請參閱標記延伸和 WPF XAMLFor more information, see Markup Extensions and WPF XAML.

Binding 是一種典型的標記延伸模組,其中,為 WPF 的 XAML 執行擴充功能的 Binding 類別,也會執行與 XAML 無關的數個其他方法和屬性。Binding is an atypical markup extension in that the Binding class that implements the extension functionality for WPF's XAML implementation also implements several other methods and properties that are not related to XAML. 其他成員的目的是要讓 Binding 更多用途和獨立的類別,除了當做 XAML 標記延伸之外,還可以處理許多資料系結案例。The other members are intended to make Binding a more versatile and self-contained class that can address many data binding scenarios in addition to functioning as a XAML markup extension.

請參閱See also