CompiledBindings CompiledBindings.WPF [] (https://www.nuget.org/packages/CompiledBindings.WPF/) CompiledBindings.XF [] (https://www.nuget.org…
CompiledBindings CompiledBindings.WPF [] (https://www.nuget.org/packages/CompiledBindings.WPF/) CompiledBindings.XF [] (https://www.nuget.org…
This library provides {x:Bind} Markup Extension for WPF, MAUI and Xamarin Forms. You can read about {x:Bind} Markup Extension for UWP here. Most functionality of {x:Bind} for UWP and also many other features are supported.
At XAML compile time, {x:Bind} is converted into C# code. Thus you can't use it in Visual Basis projects.
{x:Bind} Markup Extension have an expression as its first parameter, or the expression is specified by the Path parameter, following by other parameters like Mode, BindBack, Converter, ConverterParameter.
Note, that in some examples bellow the TextBlock (WPF) control is used, in others Label (Xamarin Forms).
Property paths
Function calls
The first example above is the call of instance method Trim(). The second is the call of static method GenerateSongTitle of MainPage class, wich takes two parameters (multi-binding).
The expression of the {x:Bind} can also have the following operators:
Note, that the Collapsed and Visible values here are inferred from Visibility property.
In the example, if the SelectedItem is not a Movie, empty text is set.
The is-operator can be used, if you have an expression and you need to compare it with many other expressions. It allows to avoid repeating the compared expression. For example, you have this expression:
IntProp1 eq 0 or IntProp1 eq 1With is-operator you can have the IntProp1 expression only once:
IntProp1 is 0 or 1In the example above the IntProp1 is compared whether it is equal to 0 or 1. If you need to do other comparisons, like greater, lower, you use the comparison operator before the right expressions. For example:
IntProp1 is ge 0 and le 10The "eq" operator is still valid. The following two expressions are the same
IntProp1 is eq 0 or eq 10
IntProp1 is 0 or 10The "not" keyword can be used before comparison operator to negate the expression:
ListProp.Count is not gt 0To compare whether the left expression is not equal, you can use either "not" or "ne" or "not eq" keywords:
IntProp1 is not 0
IntProp1 is ne 0
IntProp1 is not eq 0The is-operator is like the is matching operator in C# with the difference, that on the right side you can have any expression, not only constants. For example you can compare the IntProp1 with other properties
IntProp1 is gt 0 and le (IntProp2 + 1)The right expression of the is operator continues untill there are "and" or "or" operators. If you need to do some other compares after the is-operator, you have to include the whole is-operator expression in parens. For example, if you have the following expression:
IntProp1 is 0 or 1 and IntProp2 eq 3the parser consinders the "IntProp2" as bellonging to the is-operator. So the following C# expression will be generated:
(IntProp1 == 0 || IntProp1 == 1 && IntProp1 == IntProp2) == 3which is of course not valid. If you have this:
(IntProp1 is 0 or 1) and IntProp2 eq 3the following correct C# expression will be generated:
(IntProp1 == 0 || IntProp1 == 1) && IntProp2 == 3Like in C#, the is-operator also supports checking whether the left expression is of a given type. For example:
ObjProp is system:String or not system:Int32Contants
You can use following constants in the expression:
If not specified, the data source of {x:Bind} is the root control/page/window itself. In Xamarin Forms you can specify the data source type with x:DataType attribute.
Because x:DataType attribute is not available for WPF, you can do the following.
xmlns:mx="http://compiledbindings.com/x" mc:Ignorable="d mx"xmlns:local="clr-namespace:CompiledBindingsDemo"
mx:DataType="local:MainViewModel"Note, that if the data source is specified, the {x:Bind} extensions are only applied if the DataContext (or BindingContext in XF) of the root or corresponding controls is set to an object of the specified type.
If the {x:Bind} Markup Extension is used in a DataTemplate and you don't set the IsItemsSource property to true to the binding, setting the ItemsSource of a collection control (see below), you must specify the data type. For Xamarin Forms with x:DataType attribute. For WPF either with DataType attribute, or alternative with mx:DataType attribute.
You can change the data type anywhere in XAML by setting x:DataType (mx:DataType). You can also use {x:Null} as DataType, except in DataTemplates, to reset the data type. Note, that {x:Null} works differently for standard {Binding} and {x:Bind} extensions. For the first one, it turns off producing compiled binding at compile time, so the expression is only resolved at runtime. For the second one, it sets the data type of the control/page/window itself.
DataType property
You can set the DataType only for a one x:Bind, by setting the corresponding DataType property.
IsItemsSource property
If you use {x:Bind} to set the ItemsSource property of a collection control (ListBox, ListView, CollectionView etc), you can set the IsItemsSource property to true, so that the data type of a elements is automatically detected. Than you don't have to specify it for the ItemTemplate.
Note, that it works only if the DataTemplate is specifed as a XAML child to the collection control.
The Converter, ConverterParameter, FallbackValue and TargetNullValue can be either an expression, or a {StaticResource } markup extension.
If the Mode is not OneTime or OneWayToSource, than a code is generated to observe changes of properties in the {x:Bind} expression. The changes are observed to Dependency Properties, if there are any in the expression, as well as to objects of classes, implementing INotifyPropertyChanged interface.
\. and /. operators
Checking whether a class implements the INotifyPropertyChanged is done during compile time. With the \. (backslash dot) operator before a property you can force checking at runtime whether the source class implements the interface.
For example, you have a ListBox, and you need to bind to SelectedItems.Count property. The SelectedItems property is of type IList, so normally no listening to changes of Count property is generated. By using \. operator the code is generated to check at runtime, whether the object returned by the property implements INotifyPropertyChanged.
You can use the /. operator to stop listening to property changes. In the example above, the SelectedItems property is the dependency one, so normally the code is generated to check whether the property is changed. But actually the object (collection) returned by the property never changes. Only the collection itselft can change. So to optimize code generation you can use this:
get-only properties and ReadOnlyAttribute
To optimize the generated code, no code is generated for listining to changes of auto-generated get-only properties. For example:
class EntityViewModel : INotifyPropertyChanged
{
public EntityViewModel(EntityModel model)
{
Model = model;
}
public EntityModel Model { get; }
}So actually the Model property cannot be changed later, so no code is generated for listining whether the property is changed.
You can use the ReadOnlyAttribute for a property with false or true, to force generation or not generation of code for property changes.
If in the example above the object returned by Model property is changed, but it doesn't itselft implement INotifyPropertyChanged, you can want to force refreshing bindings by raising the PropertyChanged event with the "Model" property name. In this case you can force generating listing code with the [ReadOnly(false)] attribute:
[ReadOnly(false)]
public EntityModel Model { get; }In contrast, if a property is not an auto-generated get-only one, but you know that it never changes, you can optimize code generation by turning notifications for this property off.
[ReadOnly(true)]
public string Title => // some logicYou can use expressions, which or parts of which can return nulls. The bindings code generator determes which expressions can be null, and generates corresponding handling of them. But note! For null reference types, which are decleared as not-nullable, no nullibility checking is generated.
If a property's or a function's returning type is Task<T>, its value is taking with await.
For example, you have a function LoadImageAsync in your ViewModel, which asynchronously downloads an image. You can set the image like this:
While waiting for the value to arrive, the {x:Bind} reports the FallbackValue, if one is available, or the default value of the binding targ
暂无开放 Issues,或尚未同步最近议题。