microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Proposal: x:Bind should support calling another function within its parameter list #2407

Open michael-hawker opened 4 years ago

michael-hawker commented 4 years ago

Proposal: x:Bind functions should support calling another function within its parameter list

From comment here, Related #1630

Summary

It's difficult to do complex expressions in x:Bind to composite to UI triggers/converter replacement type scenarios from back-end model data.

Rationale

Scope

Capability Priority
Able to call another local/static function within one or more parameter arguments to another local/static function within an x:Bind expression Must
Inner functions can also accept the same binding or constant expression parameters that x:Bind accepts today Must
No (practical) limit on the number of nested functions called Should
Able to continue a chain on to the function expression with other method calls e.g. {x:Bind myns:StaticClass.DoConversion(ViewModel.Data).ToString()} Should
Can also do the same in the bindback expression Should

Examples

    {x:Bind myns:StaticClass.DoConversion(ViewModel.Data).ToArrayHelper().ToString()}
    {x:Bind myns:Helpers.Compare(Data.ToString(), "SomeString", Comparison.Equal)}
    {x:Bind myns:Helpers.ToVisibility(LookupValue(MyData.SomeProperty))}
    {x:Bind MakeBrush(ReadTextColor(Data.HexStringColor))}
    {x:Bind myns:StaticClass.Food(myns:StaticClass.Bar(ViewModel))}

Open Questions

michael-hawker commented 4 years ago

Just hit this again trying to do some Graph work with binding Query Options, would have been nice to do this:

<wgt:QueryOption
          Name="endDateTime"
          Value="{x:Bind system:DateTimeOffset.Now.Date.ToUniversalTime().AddDays(3).ToString('o', global:CultureInfo.InvariantCulture)}"/>

Unfortunately, it's not possible without this issue, so I have to break it up into two separate properties which is a pain.

Also, x:Bind/compiler doesn't seem to find the ToUniversalTime or AddDays methods? That seems like another bug, where should I file that? i.e. just trying to {x:Bind system:DateTimeOffset.Now.Date.ToUniversalTime()} fails.