{x:Bind} 標記延伸{x:Bind} markup extension

注意  如需有關在您的應用程式中使用資料系結與 {x:Bind} (的一般資訊,以及 {X:Bind}{binding}) 之間的整體比較,請參閱 深入資料系結。Note  For general info about using data binding in your app with {x:Bind} (and for an all-up comparison between {x:Bind} and {Binding}), see Data binding in depth.

{x:Bind} 標記延伸 (Windows 10 的新功能) 是 {Binding} 的替代項目。The {x:Bind} markup extension—new for Windows 10—is an alternative to {Binding}. {x:Bind} 執行的時間比 {Binding} 少很多,而且支援更佳的調試。{x:Bind} runs in less time and less memory than {Binding} and supports better debugging.

在 XAML 編譯時間,{x:Bind} 會轉換為可取得來自資料來源上之屬性值的程式碼,並將它設定在標記中指定的屬性上。At XAML compile time, {x:Bind} is converted into code that will get a value from a property on a data source, and set it on the property specified in markup. 您可以選擇性地設定繫結物件,以便觀察資料來源屬性值的變更,並根據這些變更自我重新整理 (Mode="OneWay")。The binding object can optionally be configured to observe changes in the value of the data source property and refresh itself based on those changes (Mode="OneWay"). 您也可以選擇性地設定繫結物件,以便將自己的值中的變更推回到來源屬性 (Mode="TwoWay")。It can also optionally be configured to push changes in its own value back to the source property (Mode="TwoWay").

{x:Bind}{Binding} 建立的繫結物件在功能上大致相同。The binding objects created by {x:Bind} and {Binding} are largely functionally equivalent. {x:Bind} 會執行它在編譯階段產生的特殊用途程式碼,而 {Binding} 會使用一般用途的執行階段物件檢查。But {x:Bind} executes special-purpose code, which it generates at compile-time, and {Binding} uses general-purpose runtime object inspection. 因此,{x:Bind} 繫結 (通常指已編譯的繫結) 具有高度效能,可在編譯時驗證您的繫結運算式,並讓您在產生做為頁面部分類別的原始程式碼中設定中斷點進行偵錯,而支援偵錯功能。Consequently, {x:Bind} bindings (often referred-to as compiled bindings) have great performance, provide compile-time validation of your binding expressions, and support debugging by enabling you to set breakpoints in the code files that are generated as the partial class for your page. 您可以在 obj 資料夾中找到這些檔案,其名稱類似於 (適用於 C#) <view name>.g.csThese files can be found in your obj folder, with names like (for C#) <view name>.g.cs.

提示

{x:Bind} 的預設模式為 OneTime,不同於 {Binding},其預設模式為 OneWay{x:Bind} has a default mode of OneTime, unlike {Binding}, which has a default mode of OneWay. 這是基於效能考量所選擇,因為使用 OneWay 會導致產生更多程式碼來連結和處理變更偵測。This was chosen for performance reasons, as using OneWay causes more code to be generated to hookup and handle change detection. 您可以明確地指定模式使用 OneWay 或 TwoWay 繫結。You can explicitly specify a mode to use OneWay or TwoWay binding. 您也可以使用 x:DefaultBindMode 變更標記樹狀結構特定片段的 {x:Bind} 預設模式。You can also use x:DefaultBindMode to change the default mode for {x:Bind} for a specific segment of the markup tree. 指定的模式會在該元素及其子系上,套用任何未明確指定模式做為繫結之一部分的 {x:Bind} 運算式。The specified mode applies to any {x:Bind} expressions on that element and its children, that do not explicitly specify a mode as part of the binding.

示範 {x:Bind} 的範例應用程式Sample apps that demonstrate {x:Bind}

XAML 屬性用法XAML attribute usage

<object property="{x:Bind}" .../>
-or-
<object property="{x:Bind propertyPath}" .../>
-or-
<object property="{x:Bind bindingProperties}" .../>
-or-
<object property="{x:Bind propertyPath, bindingProperties}" .../>
-or-
<object property="{x:Bind pathToFunction.functionName(functionParameter1, functionParameter2, ...), bindingProperties}" .../>
詞彙Term 描述Description
propertyPathpropertyPath 指定繫結屬性路徑的字串。A string that specifies the property path for the binding. 如需詳細資訊,請參閱下面屬性路徑一節。More info is in the Property path section below.
bindingPropertiesbindingProperties
propName =value [ 、 propName = ]*propName=value[, propName=value]* 使用名稱/值對語法指定的一或多個繫結屬性。One or more binding properties that are specified using a name/value pair syntax.
propNamepropName 要在繫結物件上設定之屬性的字串名稱。The string name of the property to set on the binding object. 例如,"Converter"。For example, "Converter".
valuevalue 設定屬性使用的值。The value to set the property to. 引數的語法取決於目前設定的屬性。The syntax of the argument depends on the property being set. 以下是_propName_ = _值_用法的範例,其中值本身是標記延伸: Converter={StaticResource myConverterClass}Here's an example of a propName=value usage where the value is itself a markup extension: Converter={StaticResource myConverterClass}. 如需詳細資訊,請參閱以下的您可以使用 {x:Bind} 設定的屬性一節。For more info, see Properties that you can set with {x:Bind} section below.

範例Examples

<Page x:Class="QuizGame.View.HostView" ... >
    <Button Content="{x:Bind Path=ViewModel.NextButtonText, Mode=OneWay}" ... />
</Page>

此範例 XAML 會搭配使用 {x:Bind}ListView.ItemTemplate 屬性。This example XAML uses {x:Bind} with a ListView.ItemTemplate property. 請注意 x:DataType 值的宣告。Note the declaration of an x:DataType value.

  <DataTemplate x:Key="SimpleItemTemplate" x:DataType="data:SampleDataGroup">
    <StackPanel Orientation="Vertical" Height="50">
      <TextBlock Text="{x:Bind Title}"/>
      <TextBlock Text="{x:Bind Description}"/>
    </StackPanel>
  </DataTemplate>

屬性路徑Property path

PropertyPath 會為 {x:Bind} 運算式設定 PathPropertyPath sets the Path for an {x:Bind} expression. Path 是一個屬性路徑,會指定您要繫結至 (來源) 的屬性、子屬性、欄位或方法的值。Path is a property path specifying the value of the property, sub-property, field, or method that you're binding to (the source). 您可以明確地提及 Path 屬性的名稱:{x:Bind Path=...}You can mention the name of the Path property explicitly: {x:Bind Path=...}. 或者,您可以將它省略:{x:Bind ...}Or you can omit it: {x:Bind ...}.

屬性路徑解析Property path resolution

{x:Bind} 不會使用 DataContext 做為預設來源,而是使用頁面或使用者控制項本身。{x:Bind} does not use the DataContext as a default source—instead, it uses the page or user control itself. 因此,它會在您的頁面或使用者控制項的程式碼後置中尋找屬性、欄位及方法。So it will look in the code-behind of your page or user control for properties, fields, and methods. 若要向 {x:Bind} 公開您的檢視模型,一般會採用的方式是將新的欄位或屬性新增到頁面或使用者控制項的程式碼後置。To expose your view model to {x:Bind}, you will typically want to add new fields or properties to the code behind for your page or user control. 屬性路徑中的步驟會使用句點 (.) 隔開,您可以納入多個分隔符號來周遊連續的子屬性。Steps in a property path are delimited by dots (.), and you can include multiple delimiters to traverse successive sub-properties. 使用句點分隔符號,無論用來實作繫結目標物件的程式設計語言為何。Use the dot delimiter regardless of the programming language used to implement the object being bound to.

例如:在頁面中,Text="{x:Bind Employee.FirstName}" 會先在頁面上尋找 Employee 成員,然後在 Employee 所傳回的物件上尋找 FirstName 成員。For example: in a page, Text="{x:Bind Employee.FirstName}" will look for an Employee member on the page and then a FirstName member on the object returned by Employee. 如果您是要將項目控制項繫結到包含員工相依項的屬性,則您的屬性路徑可能會是 "Employee.Dependents",而項目控制項的項目範本會負責顯示 "Dependents" 中的項目。If you are binding an items control to a property that contains an employee's dependents, your property path might be "Employee.Dependents", and the item template of the items control would take care of displaying the items in "Dependents".

針對 C++/CX,{x:Bind} 無法繫結至頁面或資料模型中的私用欄位和屬性;您必須使用公用屬性才可加以繫結。For C++/CX, {x:Bind} cannot bind to private fields and properties in the page or data model – you will need to have a public property for it to be bindable. 繫結的表面區域必須公開為 CX 類別/介面,以便我們取得相關的中繼資料。The surface area for binding needs to be exposed as CX classes/interfaces so that we can get the relevant metadata. 不需要可系**結屬性。 [ ] **The [Bindable] attribute should not be needed.

使用 x:Bind 時,您無須以 ElementName=xxx 做為繫結運算式的一部分。With x:Bind, you do not need to use ElementName=xxx as part of the binding expression. 相反地,您可以使用專案的名稱作為系結路徑的第一個部分,因為命名元素會變成頁面或使用者控制項內代表根系結來源的欄位。Instead, you can use the name of the element as the first part of the path for the binding because named elements become fields within the page or user control that represents the root binding source.

集合Collections

如果資料來源是一個集合,則屬性路徑可以依據項目的位置或索引來指定集合中的項目。If the data source is a collection, then a property path can specify items in the collection by their position or index. 例如,「團隊 [ 0」 ] 。「播放程式」,其中常 [ ] 值 "" 會括住 "0",要求以零索引的集合中的第一個專案。For example, "Teams[0].Players", where the literal "[]" encloses the "0" that requests the first item in a zero-indexed collection.

若要使用索引子,模型必須對要編製索引的屬性類型實作 IList<T>IVector<T>To use an indexer, the model needs to implement IList<T> or IVector<T> on the type of the property that is going to be indexed. (請注意,IReadOnlyList < t > 和 IVectorView < t > 不支援索引子語法。 ) 如果已編制索引之屬性的類型支援 INotifyCollectionChangedIObservableVector ,且系結為單向或 TwoWay,則會在這些介面上註冊並接聽變更通知。(Note that IReadOnlyList<T> and IVectorView<T> do not support the indexer syntax.) If the type of the indexed property supports INotifyCollectionChanged or IObservableVector and the binding is OneWay or TwoWay, then it will register and listen for change notifications on those interfaces. 變更偵測邏輯會根據所有的集合變更進行更新,即使不會影響特定的索引值亦然。The change detection logic will update based on all collection changes, even if that doesn’t affect the specific indexed value. 這是因為所有集合執行個體的接聽邏輯是通用的。This is because the listening logic is common across all instances of the collection.

如果資料目錄為字典或地圖,則屬性路徑可以依它們的字串名稱指定集合中的項目。If the data source is a Dictionary or Map, then a property path can specify items in the collection by their string name. 例如** < TextBlock Text = "{x:Bind player [ ' john smith ' ] }"/ > **會尋找字典中名為 "john smith" 的專案。For example <TextBlock Text="{x:Bind Players['John Smith']}" /> will look for an item in the dictionary named "John Smith". 名稱必須加上引號,而且可以使用單引號或雙引號。The name needs to be enclosed in quotes, and either single or double quotes can be used. 上標三角 (^) 可以用來逸出字串中的引號。Hat (^) can be used to escape quotes in strings. 通常最簡單的方式是使用替代引號,從用於 XAML 屬性的引號。It's usually easiest to use alternate quotes from those used for the XAML attribute. (請注意,Ireadonlydictionary<string < t > 和 IMapView < t > 不支援索引子語法。 ) (Note that IReadOnlyDictionary<T> and IMapView<T> do not support the indexer syntax.)

若要使用字串索引子,模型必須在要編製索引的屬性類型上實作 IDictionary<string, T>IMap<string, T>To use an string indexer, the model needs to implement IDictionary<string, T> or IMap<string, T> on the type of the property that is going to be indexed. 如果已編製索引的屬性類型支援 IObservableMap,且繫結為 OneWay 或 TwoWay,則它會登錄並接聽那些介面的變更通知。If the type of the indexed property supports IObservableMap and the binding is OneWay or TwoWay, then it will register and listen for change notifications on those interfaces. 變更偵測邏輯會根據所有的集合變更進行更新,即使不會影響特定的索引值亦然。The change detection logic will update based on all collection changes, even if that doesn’t affect the specific indexed value. 這是因為所有集合執行個體的接聽邏輯是通用的。This is because the listening logic is common across all instances of the collection.

附加屬性Attached Properties

若要系結至 附加屬性,您必須將類別和屬性名稱放在點之後的括弧中。To bind to attached properties, you need to put the class and property name into parentheses after the dot. 例如 Text="{x:Bind Button22.(Grid.Row)}"For example Text="{x:Bind Button22.(Grid.Row)}". 如果屬性未在 Xaml 命名空間中宣告,您將需要以 xml 命名空間為它加上首碼,而此命名空間應對應於文件最前面的程式碼命名空間。If the property is not declared in a Xaml namespace, then you will need to prefix it with a xml namespace, which you should map to a code namespace at the head of the document.

轉型Casting

編譯的繫結屬於強式類型,會解析路徑中每個步驟的類型。Compiled bindings are strongly typed, and will resolve the type of each step in a path. 如果傳回的類型沒有成員,將會在編譯時失敗。If the type returned doesn’t have the member, it will fail at compile time. 您可以指定轉換,以向繫結指出物件的真實類型。You can specify a cast to tell binding the real type of the object.

在下列案例中,obj 是類型物件的屬性,但是包含文字方塊,因此我們可以使用 Text="{x:Bind ((TextBox)obj).Text}"Text="{x:Bind obj.(TextBox.Text)}"In the following case, obj is a property of type object, but contains a text box, so we can use either Text="{x:Bind ((TextBox)obj).Text}" or Text="{x:Bind obj.(TextBox.Text)}".

groups3 Text = "{X:Bind ( # B1 data: SampleDataGroup) groups3 0) 中的 groups3 欄位 [ ] 。Title} " 是物件的字典,因此您必須將它轉換成資料: SampleDataGroupThe groups3 field in Text="{x:Bind ((data:SampleDataGroup)groups3[0]).Title}" is a dictionary of objects, so you must cast it to data:SampleDataGroup. 請注意將物件類型對應至不屬於預設 XAML 命名空間的命名空間時,所使用的 xml data: 命名空間首碼。Note the use of the xml data: namespace prefix for mapping the object type to a code namespace that isn't part of the default XAML namespace.

注意:C# 風格的轉換語法比附加的屬性語法更具彈性,且是接下來所有工作的建議語法。Note: The C#-style cast syntax is more flexible than the attached property syntax, and is the recommended syntax going forward.

檔名轉換Pathless casting

原生系結剖析器不會提供用來表示為函式參數的關鍵字 this ,但它確實支援檔名轉型 (例如, {x:Bind (x:String)}) ,可用來做為函式參數。The native bind parser doesn't provide a keyword to represent this as a function parameter, but it does support pathless casting (for example, {x:Bind (x:String)}), which can be used as a function parameter. 因此,這 {x:Bind MethodName((namespace:TypeOfThis))} 是執行在概念上相當於的有效方式 {x:Bind MethodName(this)}Therefore, {x:Bind MethodName((namespace:TypeOfThis))} is a valid way to perform what is conceptually equivalent to {x:Bind MethodName(this)}.

範例:Example:

Text="{x:Bind local:MainPage.GenerateSongTitle((local:SongItem))}"

<Page
    x:Class="AppSample.MainPage"
    ...
    xmlns:local="using:AppSample">

    <Grid>
        <ListView ItemsSource="{x:Bind Songs}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:SongItem">
                    <TextBlock
                        Margin="12"
                        FontSize="40"
                        Text="{x:Bind local:MainPage.GenerateSongTitle((local:SongItem))}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>
namespace AppSample
{
    public class SongItem
    {
        public string TrackName { get; private set; }
        public string ArtistName { get; private set; }

        public SongItem(string trackName, string artistName)
        {
            ArtistName = artistName;
            TrackName = trackName;
        }
    }

    public sealed partial class MainPage : Page
    {
        public List<SongItem> Songs { get; }
        public MainPage()
        {
            Songs = new List<SongItem>()
            {
                new SongItem("Track 1", "Artist 1"),
                new SongItem("Track 2", "Artist 2"),
                new SongItem("Track 3", "Artist 3")
            };

            this.InitializeComponent();
        }

        public static string GenerateSongTitle(SongItem song)
        {
            return $"{song.TrackName} - {song.ArtistName}";
        }
    }
}

繫結路徑中的函式Functions in binding paths

從 Windows 10 版本 1607 開始, {x:Bind} 支援使用函式作為繫結路徑的分葉步驟。Starting in Windows 10, version 1607, {x:Bind} supports using a function as the leaf step of the binding path. 這是一項功能強大的資料系結功能,可在標記中啟用數個案例。This is a powerful feature for databinding that enables several scenarios in markup. 如需詳細資料,請參閱 函數 系結。See function bindings for details.

事件繫結Event Binding

事件繫結是已編譯繫結的獨特功能。Event binding is a unique feature for compiled binding. 它可讓您使用繫結指定事件的處理常式,而無須將它做為程式碼後置上的方法。It enables you to specify the handler for an event using a binding, rather than it having to be a method on the code behind. 例如:Click="{x:Bind rootFrame.GoForward}"For example: Click="{x:Bind rootFrame.GoForward}".

事件的目標方法不可以多載,且必須:For events, the target method must not be overloaded and must also:

  • 符合事件的簽章。Match the signature of the event.
  • 或不具參數。OR have no parameters.
  • 或屬於下列類型的參數數量是相同的:可從事件參數的類型指派的參數。OR have the same number of parameters of types that are assignable from the types of the event parameters.

在產生的程式碼後置中,編譯的繫結會處理事件並將其傳送到模型上的方法,以在事件發生時評估繫結運算式的路徑。In generated code-behind, compiled binding handles the event and routes it to the method on the model, evaluating the path of the binding expression when the event occurs. 這表示它不會追蹤模型的變更,這一點與屬性繫結不同。This means that, unlike property bindings, it doesn’t track changes to the model.

如需屬性路徑之字串語法的詳細資訊,請參閱 Property-path 語法,並留意此處針對 {x:Bind} 所說明的差異。For more info about the string syntax for a property path, see Property-path syntax, keeping in mind the differences described here for {x:Bind}.

您可以使用 {x:Bind} 設定的屬性Properties that you can set with {x:Bind}

{x:Bind}bindingProperties 預留位置語法進行說明,因為在標記延伸中可以設定多個讀取/寫入屬性。{x:Bind} is illustrated with the bindingProperties placeholder syntax because there are multiple read/write properties that can be set in the markup extension. 您可以使用以逗號分隔的propName值組,以任何順序設定屬性 = valueThe properties can be set in any order with comma-separated propName=value pairs. 請注意,不可在繫結運算式中加入分行符號。Note that you cannot include line breaks in the binding expression. 某些屬性需要不具類型轉換的類型,因此這些屬性需要將自己的標記延伸巢狀在 {x:Bind} 內。Some of the properties require types that don't have a type conversion, so these require markup extensions of their own nested within the {x:Bind}.

這些屬性的運作方式與 Binding 類別的屬性十分類似。These properties work in much the same way as the properties of the Binding class.

屬性Property 說明Description
路徑Path 請參閱先前的屬性路徑一節。See the Property path section above.
ConverterConverter 指定繫結引擎呼叫的轉換器物件。Specifies the converter object that is called by the binding engine. 轉換器可以在 XAML 中設定,但若您參考已在 {StaticResource} 標記延伸中指派的物件執行個體,請在資源字典中參考該物件。The converter can be set in XAML, but only if you refer to an object instance that you've assigned in a {StaticResource} markup extension reference to that object in the resource dictionary.
ConverterLanguageConverterLanguage 指定轉換器要使用的文化特性Specifies the culture to be used by the converter. (如果您要設定 ConverterLanguage,則也應該設定 Converter)。文化特性可以設定為標準式識別碼。(If you're setting ConverterLanguage you should also be setting Converter.) The culture is set as a standards-based identifier. 如需詳細資訊,請參閱 ConverterLanguageFor more info, see ConverterLanguage.
ConverterParameterConverterParameter 指定可用於轉換器邏輯的轉換器參數Specifies the converter parameter that can be used in converter logic. (如果您要設定 ConverterParameter,則也應該設定 Converter)。大多數轉換器都可以使用簡單邏輯,從傳遞的值中取得所需的所有資訊進行轉換,而且不需要 ConverterParameter 值。(If you're setting ConverterParameter you should also be setting Converter.) Most converters use simple logic that get all the info they need from the passed value to convert, and don't need a ConverterParameter value. ConverterParameter 參數適用於中度進階轉換器實作,其中具備一個以上使用 ConverterParameter 傳遞內容的邏輯。The ConverterParameter parameter is for moderately advanced converter implementations that have more than one logic that keys off what's passed in ConverterParameter. 您可以撰寫一個使用字串以外的值的轉換器,但這並不常見,請參閱 ConverterParameter 中的<備註>,以了解詳細資訊。You can write a converter that uses values other than strings but this is uncommon, see Remarks in ConverterParameter for more info.
FallbackValueFallbackValue 指定當無法解析來源或路徑時,所要顯示的值。Specifies a value to display when the source or path cannot be resolved.
模式Mode 指定繫結模式,如下列其中一個字串:"OneTime"、"OneWay" 或 "TwoWay"。Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". 預設值是 "OneTime"。The default is "OneTime". 請注意,此與 {Binding} 的預設值 (通常為 "OneWay") 不同。Note that this differs from the default for {Binding}, which is "OneWay" in most cases.
TargetNullValueTargetNullValue 指定當來源值解析結果明確為 null 時,所要顯示的值。Specifies a value to display when the source value resolves but is explicitly null.
BindBackBindBack 指定要針對雙向繫結的相反方向使用的函式。Specifies a function to use for the reverse direction of a two-way binding.
UpdateSourceTriggerUpdateSourceTrigger 指定何時將變更從控制項推送回 TwoWay 繫結中的模型。Specifies when to push changes back from the control to the model in TwoWay bindings. 除了 TextBox 以外的所有屬性預設值。 Text 為 PropertyChanged;TextBox。文字為 LostFocus。The default for all properties except TextBox.Text is PropertyChanged; TextBox.Text is LostFocus.

注意

如果要將標記從 {Binding} 轉換成 {x:Bind},請留意 Mode 屬性的預設值不同。If you're converting markup from {Binding} to {x:Bind}, then be aware of the differences in default values for the Mode property. x:DefaultBindMode 可用來變更標記樹狀結構特定片段的 x:Bind 預設模式。x:DefaultBindMode can be used to change the default mode for x:Bind for a specific segment of the markup tree. 選取的模式將會在該元素及其子系上,套用任何未明確指定模式做為繫結之一部分的 x:Bind 運算式。The mode selected will apply any x:Bind expressions on that element and its children, that do not explicitly specify a mode as part of the binding. OneTime 效能優於 OneWay,因為使用 OneWay 將會導致產生更多程式碼來連結和處理變更偵測。OneTime is more performant than OneWay as using OneWay will cause more code to be generated to hookup and handle the change detection.

備註Remarks

由於 {x:Bind} 會使用產生的程式碼來達成其效益,因此在編譯時需要使用類型資訊。Because {x:Bind} uses generated code to achieve its benefits, it requires type information at compile time. 這表示您無法繫結至您未事先得知類型的屬性。This means that you cannot bind to properties where you do not know the type ahead of time. 因此,您無法搭配使用 {x:Bind}DataContext 屬性,因為後者屬於 Object 類型,也可能在執行階段有所變更。Because of this, you cannot use {x:Bind} with the DataContext property, which is of type Object, and is also subject to change at run time.

使用 {x:Bind} 搭配資料範本時,您必須藉由設定 x:DataType 值來指出要系結的類型,如 範例 一節所示。When using {x:Bind} with data templates, you must indicate the type being bound to by setting an x:DataType value, as shown in the Examples section. 您也可以將類型設為介面或基底類別類型,然後在必要時使用轉換來編寫完整的運算式。You can also set the type to an interface or base class type, and then use casts if necessary to formulate a full expression.

已編譯的繫結取決於程式碼產生。Compiled bindings depend on code generation. 因此,如果您在資源字典中使用 {x:Bind},則資源字典需要具備程式碼後製類別。So if you use {x:Bind} in a resource dictionary then the resource dictionary needs to have a code-behind class. 如需程式碼範例,請參閱資源字典搭配 {x:Bind}See Resource dictionaries with {x:Bind} for a code example.

包含已編譯繫結的頁面與使用者控制項,在產生的程式碼中將會有 "Bindings" 屬性。Pages and user controls that include Compiled bindings will have a "Bindings" property in the generated code. 這包括下列方法︰This includes the following methods:

  • Update() - 這會更新所有已編譯繫結的值。Update() - This will update the values of all compiled bindings. 任何單向/雙向繫結都會連結接聽程式以偵測變更。Any one-way/Two-Way bindings will have the listeners hooked up to detect changes.
  • Initialize() - 如果繫結尚未初始化,則它會呼叫 Update() 來初始化繫結Initialize() - If the bindings have not already been initialized, then it will call Update() to initialize the bindings
  • StopTracking() - 這會解除連結針對單向與雙向繫結建立的所有接聽程式。StopTracking() - This will unhook all listeners created for one-way and two-way bindings. 它們可以使用 Update() 方法重新初始化。They can be re-initialized using the Update() method.

注意

從 Windows 10 版本 1607 開始,XAML 架構針對可見度轉換器提供了內建布林值。Starting in Windows 10, version 1607, the XAML framework provides a built in Boolean to Visibility converter. 轉換器會將 true 對應至 可見 的列舉值,並將 false 轉換成折迭,如此您 就可以在不建立轉換器的情況下,將可見度屬性系結至布林值。The converter maps true to the Visible enumeration value and false to Collapsed so you can bind a Visibility property to a Boolean without creating a converter. 請注意,這不是函式繫結的功能,而是屬性繫結。Note that this is not a feature of function binding, only property binding. 若要使用內建轉換器,您 App 的最低目標 SDK 版本必須為 14393 或更新版本。To use the built in converter, your app's minimum target SDK version must be 14393 or later. 當您的 App 是以舊版 Windows 10 為目標時,您就無法使用它。You can't use it when your app targets earlier versions of Windows 10. 如需目標版本的相關詳細資訊,請參閱版本調適型程式碼For more info about target versions, see Version adaptive code.

秘訣   如果您需要為值指定單一大括弧(例如在 PathConverterParameter中),請在其前面加上反斜線: \{Tip   If you need to specify a single curly brace for a value, such as in Path or ConverterParameter, precede it with a backslash: \{. 或者,將整個字串括起來,以包含需要在設定的第二個引號中逸出的括號,例如 ConverterParameter='{Mix}'Alternatively, enclose the entire string that contains the braces that need escaping in a secondary quotation set, for example ConverterParameter='{Mix}'.

ConverterConverterLanguageConverterLanguage 皆與來自繫結來源的值或類型轉換成和繫結目標屬性相容的類型或值的案例相關。Converter, ConverterLanguage and ConverterLanguage are all related to the scenario of converting a value or type from the binding source into a type or value that is compatible with the binding target property. 如需詳細資訊和範例,請參閱深入了解資料繫結中的<資料轉換>一節。For more info and examples, see the "Data conversions" section of Data binding in depth.

{x:Bind} 只是標記延伸,無法以程式設計方式建立或操作這類繫結。{x:Bind} is a markup extension only, with no way to create or manipulate such bindings programmatically. 如需標記延伸的詳細資訊,請參閱 XAML 概觀For more info about markup extensions, see XAML overview.