CalcBinding is an advanced Binding markup extension that allows you to write calculated binding expressions in XAML, without custom converters. CalcBinding can automatically perform bool to visibility conversion, inverse your expression and more. CalcBinding makes binding expressions shorter and more user-friendly.
CalcBinding (by @Alex141) is a very useful feature. It allows us to do many common functionalities in the binding which are typically need converters. But it is only available for WPF. It will be nice to have something similar to UWP.
Here are some key features of CalcBinding (source: CalcBinding - Key features and restrictions)
One or many source properties in Path with many available operators
<Label Content="{c:Binding A*0.5+(B.NestedProp1/C - B.NestedProp2 % C) }" />
xml
<c:Binding 'A and B or C' />
One or many static properties in Path
<TextBox Text="{c:Binding 'local:StaticClass.Prop1 + local:OtherStaticClass.NestedProp.PropB + PropC'}"/>
xml
<Button Background="{c:Binding '(A > B ? media:Brushes.LightBlue : media:Brushes.White)'}"/>
Properties and methods of class System.Math in Path
<TextBox Text="{c:Binding 'Math.Sin(Math.Cos(A))'}"/>
Enum types like constants or source properties in Path
<TextBox Text="{c:Binding '(EnumValue == local:CustomEnum.Value1 ? 10 : 20)'}"/>
Automatic inversion of binding expression if it's possible
<TextBox Text = "{c:Binding 'Math.Sin(A*2)-5'}"/> {two way binding will be created}
Automatic two way convertion of bool expression to Visibility and back if target property has such type
```xml
Related issue CalcBinding#47. This issue has no hope as @Alex141 said
I really paused work on the project
+1 this is a super powerful tool to have
Thanks for sharing, wasn't aware of this library. If we were to bring this over to the toolkit, it would probably need to be rewritten from scratch - don't think the Apache 2 license is compatible with MIT (I could be wrong). Curious to see what kind of demand there is from the community
At my company we usually work with what we have. I imagine a lot of UWP developers do that and don't really make the effort to push for these kinds of features. If we could get features like these, I'm guessing we'd see a lot more complex app designs. Over here we aren't too comfortable building in third party libraries for support reasons.
At my company we always limit our design scope on UWP do to the level of involvement you have to go through to make things happen compared to iOS. We're a custom cross-platform application shop and strive to build our apps natively faster. I'd definitely say tools like these help our business and reduce code/complexity in our projects.
For example, this feature request makes it easier to place a UIElement on top of another and maintain that position when the app scales; something that's very challenging to do on Windows quickly. Lots of back and forth between code and xaml.
@jefhai have you looked at the RelativePanel?
I'm up for looking at this in more detail for 3.0+.
I've been wanting to work on some x:Bind helpers anyway (like not and simple bool and math functions), as it should really be able to perform a lot of these scenarios. (x:Bind automatically handles # 6 already in 14393+).
I've figured out how to use some of the initial Markup Extension stuff added in 16299, but it doesn't look like you can have generic input to a UWP markup extension (it needs to be quantified with a property name {local:Calc Eq=5+5}).
@michael-hawker Is it possible for v3.0? This could the main feature of v3.0 because the feature will be used in every app.
@michael-hawker is it possible I can work on this feature a bit? Or are only Microsoft contributors allowed?
@jefhai As the name (Community) suggest, everyone is more than welcome to contribute to this toolkit. For more info: Contributing to UWP Community Toolkit
@jefhai welcome aboard! We always welcome involvement from the community!
I just wanted to call out I think it's important and am happy to schedule it on my long list of things to get to if no one else does. If you start putting something together let me know. I'm happy to share my uwp markup extension template I experimented with.
I've been doing a lot of research and work with XAML bindings lately, so that was the other reason I volunteered. @Vijay-Nirmal I'll have some x:Bind helpers ready for 3.0 for sure as part of another thing I'm working on I believe. This whole request is a bit more complex then just that though, so we can coordinate as people move forward.
The advantage of having lots of tiny x:Bind helpers is they're easier to manager, and then the XAML compiler should do all the reference injection. Where as if we have our own custom markup extension, then we have to parse reference names in the string and locate the reference/resource on our own.
@jefhai Seems like it has been a while but were you able to work on this one? or any update on this?
@jefhai if you still want to work on this feature please let us know.
@Kyaa-dost I鈥檓 going to give it some thought. I鈥檒l post back here
@jefhai cool, I know the original roadblock was because UWP doesn't have the IServiceProvider access in MarkupExtensions as was used in the WPF CalcBinding project linked to above.
However, I believe WinUI 3 according to their issue is going to support it in the future. I'm not sure if it's part of their previews yet, you'd have to check, but that may be the best path forward as then we may be able to port the older WPF project instead more easily.
Most helpful comment
@Kyaa-dost I鈥檓 going to give it some thought. I鈥檒l post back here