UWP Development Mvvmlight Practice IV: {X:bind} and {Binding} differences in detail

Source: Internet
Author: User

{X:bind} was added as the UWP was introduced, which could be said to be the WIN10 UWP Development proprietary extension. Although {x:bind} lacks some of the features in {binding} , it takes less time to run and uses more memory than {binding} and supports better debugging.

Reference URL: {x:bind} markup extension, GitHub Microsoft UWP instance Xamlbind

1,{x:bind} Fundamentals

When the XAML is loaded,{x:bind} will be converted to the binding object you want, and the object will get the relevant value from a property on the data source. The bound object can be configured to observe changes to the data source property values and refresh itself based on those changes. The object can also be configured to push changes to its own value back to the Source property.

2,{x:bind}{binding} The difference between the two

    The binding objects created by
    • {x:bind} and {binding} are broadly equivalent in function.
    • {x:bind} executes the specialized code generated at compile time, while {Binding} uses a common run-time object check. The
    • {x:bind} binding, usually referred to as a compiled binding, has excellent performance, provides compile-time validation of binding expressions, and enables debugging by allowing you to set breakpoints in code files that are generated as part of a page's class. These files can be found in the obj folder, and their names are similar to (for C #) <view Name>.g.cs .
    • {x:bind} does not use DataContext as the default source-it will use either the page or the user control itself. Therefore, it finds code-behind pages or user controls for properties, fields, and methods. To display a view model to {x:bind} , you typically need to add a new field or property to a code-behind page or user control. The steps in the property path are delimited by a dot (.), and can contain multiple delimiters to traverse successive sub-properties.
    • when using x:bind , you do not need to use elementname=xxx as part of a binding expression. When you use x:bind , you can use the name of the element as the first part of the binding path, because the named element becomes a field within the page or user control that represents the root binding source. The
    • event binding is a new feature of the compilation bindings. It allows you to use bindings to specify handlers for events without having to make them a code-behind method.
    • when using {X:bind} with a data template, you can set the X:datatype value to indicate the type to which you want to bind, or set the type to an interface or a base class type. Then use the transform as needed to write a complete expression.

3,{x:bind} Basic Usage

<object property= "{x:bind}" .../>-or-<object property= "{x:bind PropertyPath}" .../>-or-<object Property= "{x:bind bindingproperties}" .../>-or-<object property= "{x:bind propertyPath, bindingproperties}" ... />

PropertyPath A string that specifies the property path of the binding
Bindingproperties

propname=value[, propname=value]*

One or more binding properties specified using a name/value pair syntax

PropName The string name of the property to set on the bound object
Value The value to set the property to. The syntax of the parameter depends on the property you want to set.

Note:PropertyPath Sets the Pathfor the {x:bind} expression. Path is a property path that specifies the value of the (source) property, child property, field, or method to bind to. You can explicitly indicate the Path property name: {Binding Path=...} . You can also omit it: {Binding ...} .

4,{x:bind} property settings

Path Property Path
Converter Specifies the converter object that the binding engine invokes. The converter can be set in XAML, but only when you reference an instance of an object that has been allocated in ResourceDictionary in XAML. You can use the {StaticResource} Markup extension reference to the object in the resource dictionary.
Converterlanguage Specifies the culture to be used by the converter. (If you want to set converterlanguage, you should also set Converter.) ) culture can be set to a criteria-based identifier. For more information, see converterlanguage.
Converterparameter Specifies the converter parameters that can be used in the converter logic. (If you want to set converterparameter, you should also set Converter.) Most converters use simple logic that can get all the required information from the pass-through values you want to convert, without the need for a converterparameter value. The converterparameter parameter is intended for medium-high-level converter implementations with multiple logic that can cut the contents of incoming converterparameter . You can write a converter that uses values other than strings, but this is not a common situation, see Remarks in converterparameter for more information.
Fallbackvalue Specifies the value to display when the source or path cannot be resolved.
Mode Specifies the binding mode as one of the following strings: "OneTime", "OneWay", or "TwoWay". The default value is "OneTime". Note that this value is not the default value for {Binding} , and in most cases "OneWay".
Targetnullvalue Specifies the value to display when the source value is resolved but not explicitly null .

Note: Onetime only as the display, OneWay realizes the INotifyPropertyChanged property receive state change, TwoWay realizes the inotifypropertychanged the interactive attribute.

5, common examples

  • text= "{X:bind employee.firstname}" binding member
  • text= "{X:bind model.employees[0]. Name} "Binding Collection Object member
  • text= "{X:bind model.managerprop.reportsoc[0]. Name, Mode=onetime} "deep binding
  • text= "{x:bind model.employees[0], mode=onetime}" binding object automatically calls the ToString () method output string
  • Value= "{x:bind model.intpropertydp, Mode=twoway, Converter={staticresource inttodouble}}"
  • Visibility= "{x:bind MODEL.BOOLPROPWITHINPC, Mode=oneway, Converter={staticresource BoolToVisibility}}"
  • text= "{X:bind LocalTextBox.Text.Length, mode=oneway}"
  • Grid.column= "{X:bind MODEL.INTPROPWITHINPC, mode=oneway}"
  • Grid.column= "{x:bind onetimeslider3.value, Mode=oneway, Converter={staticresource Doubletoint}}"
  • text= "{X:bind Background. Solidcolorbrush.color), fallbackvalue= ' This is my fallback value ', Mode=oneway} "
  • text= "{x:bind nullstringproperty, targetnullvalue= ' This was a null string property '}"
  • text= "{X:bind Button22. Grid.Row)} "binding attached property
  • text= "{x:bind obj. (textbox.text)}" obj is a property of type object that contains a text box
  • text= "{X:bind groups3[0]. ( Data:SampleDataGroup.Title)} "The Groups3 field is an object dictionary and must be converted to Data:sampledatagroup
  • click= "{x:bind Rootframe.goforward}" event binding

public void GoForward (object sender, RoutedEventArgs e) {}

private void GoForward () {}

private void GoForward (object sender, Object e) {}

For events, the target method cannot be overloaded, and you must also:

  • The signature of the matching event.
  • or without any parameters.
  • Or have the same number of parameter types, which are assigned according to the type of the event argument.

In the generated code-behind, the compiled binding handles the event and routes it to the corresponding method on the model, and evaluates the path of the binding expression when the event occurs. This means that, unlike property bindings, it does not track changes to the model.

Mvvmlight Practice for UWP development four: {X:bind} and {Binding} differences in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.