SkillAgentSearch skills...

CalcBinding

Advanced WPF Binding which supports expressions in Path property and other features

Install / Use

/learn @Alex141/CalcBinding
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

CalcBinding

CalcBinding is an advanced Binding markup extension that allows you to write calculated binding expressions in xaml, without custom converters. CalcBinding can automaticaly perfom bool to visibility convertion, different algebraic operations, inverse your expression and more. CalcBinding makes binding expressions shorter and more user-friendly. Release notes

Install

CalcBinding is available at NuGet. You can install package using:

PM> Install-Package CalcBinding 

Overview

Following example shows xaml snippets with standart Binding and with CalcBinding in very simple case:

Before:

<Label>
  <Label.Content>
  <MultiBinding Conveter={x:StaticResource MyCustomConverter}> 
    <Binding A/> 
    <Binding B/> 
    <Binding C/> 
  </MultiBinding>
  </Label.Content>
</Label> 

(without MyCustomConveter declaration and referencing to it in xaml)

After:

<Label Content="{c:Binding A+B+C }" />

Key features and restrictions:

  1. One or many source properties in Path with many available operators: description
<Label Content="{c:Binding A*0.5+(B.NestedProp1/C - B.NestedProp2 % C) }" />
<c:Binding 'A and B or C' />
  1. One or many static properties in Path: description
<TextBox Text="{c:Binding 'local:StaticClass.Prop1 + local:OtherStaticClass.NestedProp.PropB + PropC'}"/>
<Button Background="{c:Binding '(A > B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>
  1. Properties and methods of class System.Math in Path: description
<TextBox Text="{c:Binding 'Math.Sin(Math.Cos(A))'}"/>
  1. Enum types like constants or source properties in Path: description
<TextBox Text="{c:Binding '(EnumValue == local:CustomEnum.Value1 ? 10 : 20)'}"/>
  1. Automatic inversion of binding expression if it's possible: description
<TextBox Text = "{c:Binding 'Math.Sin(A*2)-5'}"/> {two way binding will be created}
  1. Automatic two way convertion of bool expression to Visibility and back if target property has such type: description
<Button Visibility="{c:Binding !IsChecked}" /> 
<Button Visibility="{c:Binding IsChecked, FalseToVisibility=Hidden}" />
  1. Other features such as string and char constants support and other: description

  2. General restrictions: description

Documentation

1. Source properties and operators

You can write any algebraic, logical and string expressions, that contain source property pathes, strings, digits, all members of class Math and following operators:

"(", ")", "+", "-", "*", "/", "%", "^", "!", "&&","||",
"&", "|", "?", ":", "<", ">", "<=", ">=", "==", "!="};

and ternary operator in form of 'bool_expression ? expression_1 : expression_2'

One should know, that xaml is generally xml format, and xml doesn't support using of following symbols when setting attribute value: &, <. Therefore, CalcBinding supports following aliases for operators that contain these symbols:

| operator | alias | comment | | -------- |:-----:| :-----:| | && | and | | | || | or | not nessesary, just for symmetry | | < | less | | | <= | less= | |

Examples

Algebraic

<TextBox Text="{c:Binding A+B+C}"/>
<TextBox Text="{c:Binding A-B-C}"/>
<TextBox Text="{c:Binding A*(B+C)}"/>
<TextBox Text="{c:Binding 2*A-B*0.5}"/>
<TextBox Text="{c:Binding A/B, StringFormat={}{0:n2} --StringFormat is used}"/> {with string format}
<TextBox Text="{c:Binding A%B}"/>
<TextBox Text="{c:Binding '(A == 1) ? 10 : 20'}"/> {ternary operator}

Logic

<CheckBox Content="!IsChecked" IsChecked="{c:Binding !IsChecked}"/>
<TextBox Text="{c:Binding 'IsChecked and IsFull'}"/> {'and' is equvalent of '&&'}
<TextBox Text="{c:Binding '!IsChecked or (A > B)'}"/> {'or' is equvalent of '||', but you can leave '||'}
<TextBox Text="{c:Binding '(A == 1) and (B less= 5)'}"/> {'less=' is equvalent of '<='}
<TextBox Text="{c:Binding (IsChecked || !IsFull)}"/>

Restrictions:

  1. Identifiers that make up the source property path, should be separated from operator ':' by any operator or delimititer (single quote, space etc.) in ternary operator:

right:

<TextBox Text="{c:Binding '(A == 2)?IsChecked : IsFull}"/> <!-- right -->
<TextBox Text="{c:Binding '(A == 2)?IsChecked :!IsFull}"/> <!-- right -->
<TextBox Text="{c:Binding '(A == 2) ? IsChecked :4 + IsFull}"/> <!-- right -->

wrong:

<TextBox Text="{c:Binding '(A == 2)?IsChecked:IsFull}"/> <!-- wrong -->

That restricition is caused by path analyzer work that finds static properties

2. Static properties

Beginning with version 2.3 CalcBinding supports static properties in binding expression. You can write pathes that begin with static property of any class and have any number of properties following behind static property. CalcBinding uses following syntax of static property path declaration:

'xmlNamespace:Class.StaticProperty.NestedProperty' etc.

where:

  1. xmlNamespace - usual xml namespace that is mapped to normal namespace in a header of xaml file with other namespaces definitions.

  2. Class - name of class that exists in namespace whereto xmlNamespace is mapped

  3. StaticProperty - static property of class Class

  4. .NestedProperty etc - chain of properties following behind StaticProperty

Examples:

<TextBox Text="{c:Binding 'local:Class.NestedProp.Prop1 + local:OtherStaticClass.PropB + PropC'}"/>
<Button Background="{c:Binding '(A > B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>

where local and media are defined in a header of xaml file:

<<UserControl x:Class="WpfExample.FifthPage"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:local="clr-namespace:WpfExample"
           xmlns:media ="clr-namespace:System.Windows.Media;assembly=PresentationCore">
   ...
</UserControl>

Restrictions

  1. As for non-static property pathes for static property pathes following rule is applied: you should put any delimiter or operator between ':' operator of ternary operator and identifiers (namespace or property) that make up static property path:

right:

<TextBox Text="{c:Binding '(A == 2)?local:Class.Prop1 : local:Class.Prop2}"/> <!-- right -->
<TextBox Text="{c:Binding '(A == 2)?local:OtherClass.IsChecked :!local.OtherClass.IsFull}"/> <!-- right -->
<TextBox Text="{c:Binding '(A == 2) ? local:Class.A :4 + local:Class.B}"/> <!-- right -->

wrong:

<TextBox Text="{c:Binding '(A == 2)?local:Class.Prop1: local:Class.Prop2}"/> <!-- wrong -->
<TextBox Text="{c:Binding '(A == 2)?local:OtherClass.IsChecked:local.OtherClass.IsFull}"/> <!-- wrong -->
<TextBox Text="{c:Binding '(A == 2) ? local:Class.A:4+local:Class.B}"/> <!-- wrong -->

3. Math class members

You can use in path property any members of System.Math class in native form as if you are writing usual C# code:

<TextBox Text="{c:Binding Math.Sin(A*Math.PI/180), StringFormat={}{0:n5}}"/>
<TextBox Text="{c:Binding A*Math.PI}" />

Restrictions

  1. Although CalcBinding supports static properties, Math class is a standalone feature that was created and used before static properties were supported. For this reason you shouldn't use static property syntax with members of Math class.

right:

<TextBox Text="{c:Binding A*Math.PI}" /> <!-- right -->
<TextBox Text="{c:Binding Math.Sin(10)+20}" /> <!-- right -->

wrong:

<xmlns:sys="clr-namespace:System;assembly=mscorlib">
...
<TextBox Text="{c:Binding A*sys:Math.PI}" /> <!-- wrong -->
<TextBox Text="{c:Binding sys:Math.Sin(10)+20}" /> <!-- wrong -->

4. Enums

Beginning with version 2.3 CalcBinding supports Enums expressions in binding expression. You can write enum values or properties that have Enum type (static properties too). CalcBinding uses following syntax of declaration enum value:

'xmlNamespace:EnumClass.Value'

where:

  1. xmlNamespace - usual xml namespace that is mapped to normal namespace in a header of xaml file with other namespaces definitions.

  2. EnumClass - name of enum class that exists in namespace whereto xmlNamespace is mapped

Examples:

<CheckBox Content="Started" IsChecked="{c:Binding 'State==local:StateEnum.Start'}" />
<Button Background="{c:Binding 'EnumValue == local:MyEnum.Value1 ? media:Brushes.Green : media:Brushes.Red'}"/>

where

  1. local and media are defined in a header of xaml file: xml <<UserControl x:Class="WpfExample.FifthPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfExample" xmlns:media ="clr-namespace:System.Windows.Media;assembly=PresentationCore"> ... </UserControl>

  2. StateEnum, MyEnum - custom Enums

  3. StateEnum.Start, MyEnum.Value1 - values of custom Enums

  4. Brushes - standart class with static Brush properties

  5. Brushes.Green, Brushes.Red - static properties of class Brushes

Restrictions

  1. As for static property pathes for Enum constants
View on GitHub
GitHub Stars703
CategoryCustomer
Updated54m ago
Forks85

Languages

C#

Security Score

100/100

Audited on Mar 29, 2026

No findings