繫結標記延伸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 屬性的只是一般 XAML 處理Binding類別。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. 若要了解的特定屬性的 XAML 屬性使用方式Binding,請參閱相關的屬性"XAML 屬性使用方式 」 一節Binding.NET Framework 類別庫中。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物件使用預設值,其中包含初始Binding.PathnullThe {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.Source正在的內容中設定執行階段資料。This 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.PathBinding標記延伸使用方式所建立。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近似值,因為有許多的讀取/寫入屬性的BindingBase或是Binding可以透過設定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.PathThey 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. 檢查 XAML 屬性使用方式區段,在.NET Framework 類別庫,針對每個屬性,如需詳細資訊;使用 XAML 屬性語法,與字串或沒有進一步的標記延伸使用方式基本上就是您在中指定的值相同Binding運算式,與未放置引號括住每個例外狀況bindProp =valueBinding運算式。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.

備註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. 相反地,您會使用 property 項目。You would instead use property elements. 請參閱參考主題MultiBindingPriorityBindingSee 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 已到達非典型的標記中的延伸模組,Binding實作 WPF 的 XAML 實作的擴充功能的類別也會實作數個其他的方法和 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