Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

CalcBinding

> 编程语言
Open source

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

707 stars0 likes0 views
WebsiteGitHub

About

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

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:


  
   
     
     
     
  
  
 

(without MyCustomConveter declaration and referencing to it in xaml)

After:

Key features and restrictions:

  1. One or many source properties in Path with many available operators: description
  1. One or many static properties in Path: description
 B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>
  1. Properties and methods of class System.Math in Path: description
  1. Enum types like constants or source properties in Path: description
  1. Automatic inversion of binding expression if it's possible: description
 {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
 
  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: **&,

{with string format}

{ternary operator}

#### Logic
```xml

 {'and' is equvalent of '&&'}
 B)'}"/> {'or' is equvalent of '||', but you can leave '||'}
 {'less=' is equvalent of '

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:

 
 
 

wrong:

 

…

xml
  
 B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>

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


   ...

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:

 
 
 

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:

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:

 
 

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:

where

  1. local and media are defined in a header of xaml file: ```xml

    ...

```  
  1. StateEnum, MyEnum - custom Enums

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

  3. Brushes - standart class with static Brush properties

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

Restrictions

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

right:

 
 

wrong:

 
 
 

5. Automatic inversion of binding expression

For examle, you have to create two way binding from viewModel with double property A and Content property of TextBox. TextBox.Content depends on property 'A' by following formula: 'Math.Sin(A*2)-5'

All you have to do is to write:

CalcBinding recognizes that this expression has inversed expression 'A = Math.Asin(TextBox.Content + 2) / 2' and will use this expression for convertion dependency property TextBox.Text to property A of ViewModel when Text of textBox changes.

Previous expression equivalents to following usual code:

public class MyMathConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          var source = (int)value;
          return Math.Sin(source*2)-5;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {        
          var res = Double.Parse(value);
          return (int)(Math.Asin(res + 5) / 2);
        }
}

Restrictions of creating inversed expression

  1. Binding must include only one property path (static or non-static) and only one entry of it

  2. Binding can contain only following operators and methods:

"+", "- (binary)", "*", "/", "Math.Sin", "Math.Cos", "Math.Tan", "Math.Asin", 
"Math.Acos", "Math.Atan","Math.Pow", "Math.Log", "!", "- (unary)"};

6. Bool to Visibility automatic convertion

CalcBinding recognizes if dependency property with Visibility type binds to bool expression. If it's true then bool expression is converted to Visibility automaticaly.

Obsiously true expression result is converted to Visibility.Visible

Property FalseToVisibility of CalcBinding specifies state in which false expression result is converted. Flag can have one of the following values:

  1. FalseToVisibility.Collapsed (default)
  2. FalseToFisibility.Hidden

Examples


or just

…

xml

However, in this case we loose the ability of supporting Char constants. Therefore beginning with version 2.3 CalcBinding has new property - SingleQuotes. If property is true, CalcBinding considers that all quotes - double and single, are single quotes. So \'A\' and &quot;A&quot; are Char symbols in that mode. If property is false, then single and double quotes are considered as double quotes, it is variant by defaults. So \'A\' and &quot;A&quot; are String constants in that mode. Examples of Char supporting:

 {can't use no \' nor &apos; symbols because of xaml compiler generates error when parses == operator}

where Symbol - Char property.

Restrictions:

  1. Simultaneous using of Char and String constants is not supported in this version.

TemplateBinding

Althouth CalcBinding hasn't yet analog for TemplateBinding, as temporary solution you can write as follow:


    
        
            
        
    
 

Setting RelativeSource property to TemplatedParent value makes CalcBinding similar to TemplateBinding

8. Tracing

All calcbinding traces are disabled by default due to huge amount of trace messages in some scenarios (see bug 44).

To enable traces, you need to specify minimal tracing level. Add this code to your app.config file to see all Information or higher priority logs:

  
    
      
    
  

…


1. I wrote logical expression A && B, A < B, A <= B, but my xaml doesn't compile, what's wrong?

As Xaml is generally xml format, some symbols are denied and one should use it's aliases instead os its. See operators aliases table in section Source properties and operators

2. I wrote string expression A + " some text", but my xaml doesn't compile, what's wrong?

In markup extension we can't use double quotes, so we can use single quotes and backslash for escaping \' or xml escape symbol &quot;. See section String, Char and SingleQuotes mode

3. Can I use CalcBinding instead of TemplateBinding?

…

xml

In older version it creates BindingMode.TwoWay binding, in new version it creates BindingMode.Default, which translated by Label.Visibility in BindingMode.OneWayMode. If you need to stay on BindingMode.TwoWay then just specify it:

…

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#bindingc-sharpwpfxaml

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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