📜 ⬆️ ⬇️

New x features: Bind in UWP

Image


In addition to extensions and many other things , Anniversary Update greatly expanded the capabilities of compiled bindings (x: Bind). Let's see what has changed.


An asterisk ( * ) will mark features that require a new SDK.


Collections


Now you can use syntax of the form {x:Bind Collection[0]} to bind to specific elements of the list. Moreover, if the source list implements INotifyCollectionChanged, and Mode is set to OneWay or TwoWay, the binding will be updated when the list changes (even if the change does not affect the bound item). To use this feature, the list must implement IList<T> or IVector<T> .


This syntax is available not only for lists, but also for dictionaries and maps - {x:Bind Dictionary['Key']} . True key type can only be string. Similar to lists, a dictionary or Map can implement an IObservableMap to update bindings. To escape quotes in a string, use the ^ character.


Attached properties


Now you can bind to the values ​​of Attached Properties. For example, {x:Bind MyButton.(Grid.Row)} bind to the line number in which MyButton is located. If Attached Property is not declared in the standard namespace, you need to add the corresponding prefix indicating the desired namespace.


Conversion *


Unlike the classic Binding, x: Bind is strongly typed, and non-valid bindings give errors in compile-time. But it so happens that you bind to a property with some declared type, but the property itself is an object of a more specific type. And even if you are sure that a member that is not in the base type is in its successor, you simply cannot be attached to this member. To solve this problem, in 1607 a casting option was added - {x:Bind ((MyObject) Property).Member} or {x:Bind Property.(MyObject.Member)} (the first option is recommended). This is also useful for converting nullable to a regular type (for example, bool? -> bool).


BooleanToVisiblityConverter *


Now not needed. Boolean values ​​will be implicitly converted to Visibility. Finally.


Feature Binding *


This is perhaps the most powerful new feature of x: Bind. Now you can bind directly to the returned values ​​of the functions. For example, {x:Bind MyNumber.ToString('F4')} will return MyNumber with 4 decimal places.



Of course, there are limitations:



As we can see, XAML, although slowly, develops, which is very pleasing. It is possible that in RS2 and RS3 will also add something new. In the meantime, you can slowly master new things and get rid of the code that is now unnecessary.


')

Source: https://habr.com/ru/post/307226/


All Articles