Advanced WPF Binding which supports expressions in Path property and other features
Advanced WPF Binding which supports expressions in Path property and other features
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
CalcBinding is available at NuGet. You can install package using:
PM> Install-Package CalcBinding
Following example shows xaml snippets with standart Binding and with CalcBinding in very simple case:
(without MyCustomConveter declaration and referencing to it in xaml)
B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>
{two way binding will be created}
Other features such as string and char constants support and other: description
General restrictions: description
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 '
…
xml
B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>
where local and media are defined in a header of xaml file:
...
You can use in path property any members of System.Math class in native form as if you are writing usual C# code:
...
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:
xmlNamespace - usual xml namespace that is mapped to normal namespace in a header of xaml file with other namespaces definitions.
EnumClass - name of enum class that exists in namespace whereto xmlNamespace is mapped
where
local and media are defined in a header of xaml file: ```xml
...
```
StateEnum, MyEnum - custom Enums
StateEnum.Start, MyEnum.Value1 - values of custom Enums
Brushes - standart class with static Brush properties
Brushes.Green, Brushes.Red - static properties of class Brushes
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);
}
}
Binding must include only one property path (static or non-static) and only one entry of it
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)"};
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:
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 "A" 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 "A" are String constants in that mode. Examples of Char supporting:
{can't use no \' nor ' symbols because of xaml compiler generates error when parses == operator}
where Symbol - Char property.
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
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 ". 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:
…
No open issues yet, or sync has not completed.