百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

CompiledBindings

> 编程语言
开源

CompiledBindings CompiledBindings.WPF [] (https://www.nuget.org/packages/CompiledBindings.WPF/) CompiledBindings.XF [] (https://www.nuget.org…

344 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

CompiledBindings CompiledBindings.WPF [] (https://www.nuget.org/packages/CompiledBindings.WPF/) CompiledBindings.XF [] (https://www.nuget.org…

CompiledBindings

CompiledBindings.WPF

CompiledBindings.XF

CompiledBindings.MAUI

CompiledBindings.WinUI

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

{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.

x:Bind usage

Note, that in some examples bellow the TextBlock (WPF) control is used, in others Label (Xamarin Forms).

Property paths

xaml

Function calls

xaml

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:

  • binary operators (in round brackets alternatives, which are better for XML): +, -, *, /, = (eq), != (ne), (gt), = (ge)
xaml
  • unary operators: -, +, ! (not)
xaml
  • logical operators: && (and), || (or)
xaml
  • coalesce operator
xaml

Note, that the Collapsed and Visible values here are inferred from Visibility property.

  • null check operator
xaml
  • cast operator. The class, to which cast is made, must be fully specified with namespace (the namespace must be declared)
xaml
  • as operator. The class, to which cast is made, must be fully specified with namespace (the namespace must be declared).
xaml

In the example, if the SelectedItem is not a Movie, empty text is set.

  • new operator. The class must be fully specified with namespace
xaml
  • interpolated string. The interpolated string must start with $ symbol following a string literal in quotes. The string may contain interpolated expressions in {} brackets, with optional format part. The string is resolved when the binding applies, or one of the values of any expression is changed.
xaml
  • is operator.

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:

xaml
IntProp1 eq 0 or IntProp1 eq 1

With is-operator you can have the IntProp1 expression only once:

xaml
IntProp1 is 0 or 1

In 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:

xaml
IntProp1 is ge 0 and le 10

The "eq" operator is still valid. The following two expressions are the same

xaml
IntProp1 is eq 0 or eq 10
IntProp1 is 0 or 10

The "not" keyword can be used before comparison operator to negate the expression:

xaml
ListProp.Count is not gt 0

To compare whether the left expression is not equal, you can use either "not" or "ne" or "not eq" keywords:

xaml
IntProp1 is not 0
IntProp1 is ne 0
IntProp1 is not eq 0

The 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

xaml
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:

xaml
IntProp1 is 0 or 1 and IntProp2 eq 3

the parser consinders the "IntProp2" as bellonging to the is-operator. So the following C# expression will be generated:

csharp
(IntProp1 == 0 || IntProp1 == 1 && IntProp1 == IntProp2) == 3

which is of course not valid. If you have this:

xaml
(IntProp1 is 0 or 1) and IntProp2 eq 3

the following correct C# expression will be generated:

csharp
(IntProp1 == 0 || IntProp1 == 1) && IntProp2 == 3

Like in C#, the is-operator also supports checking whether the left expression is of a given type. For example:

xaml
ObjProp is system:String or not system:Int32

Contants

You can use following constants in the expression:

  • numbers with or wihout decimal seperator (point). For example 2.3
  • true, false
  • null
  • this
  • strings. For example 'some string'
  • characters. The same as strings, for example 'a'. Which type is actually used is mostly inferred during compilation. In case of ambiquity, the cast operator must be used.

Data source

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.

  • Declare namespace http://compiledbindings.com/x. For example
xaml
  xmlns:mx="http://compiledbindings.com/x"
  • Set the prefix of the namespace as ignorable (in the example together with d namespace)
xaml
  mc:Ignorable="d mx"
  • Now you can set the data source type like this
xaml
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.

xaml

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.

xaml

   
       
        
    
   

Note, that it works only if the DataTemplate is specifed as a XAML child to the collection control.

x:Bind other parameters

  • Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", "TwoWay" or "OneWayToSource". The default is "OneWay"
  • Converter Specifies the converter.
  • ConverterParameter Specifies the converter parameter.
  • BindBack Specifies a expression to use for the reverse direction of a two-way binding. If the property is set, the Mode is automatically set two TwoWay.
  • FallbackValue Specifies a value to display when the source or path cannot be resolved.
  • TargetNullValue Specifies a value to display when the source value resolves but is explicitly null.
  • UpdateSourceEventNames Specifies an event, or a list of events separated by | symbol, to use for updating the binding's source.
  • StringFormat A string that specifies how to format the source value as a string.

The Converter, ConverterParameter, FallbackValue and TargetNullValue can be either an expression, or a {StaticResource } markup extension.

Observing changes

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.

xaml

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:

xaml

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:

csharp
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:

csharp
    
    [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.

csharp
    [ReadOnly(true)]
    public string Title => // some logic

Nullability checking

You 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.

Binding to asynchronous (returning Task<T>) properties and functions.

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:

xaml

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· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言