[UWP] Bug with x:bind with String.Format and two items

Rick 21 Reputation points
2020-10-20T21:26:43.823+00:00

Hi

I'm getting a compiler error when I try to use x:Bind with string.format and two items:

Invalid binding path 'sys:String.Format('{0} is now available in {0}', First,Second )' : Invalid or missmatched parameter at position '1'.

Repoduction is creating a new UWP application with the following code:

Add to the MainPage:

    <Page xmlns:sys="using:System" >  
        <Grid>  
            <TextBlock Text="{x:Bind sys:String.Format('{0}-{1}', First, Second)}" />  
        </Grid>  
    </Page>  

And the following code-behind

public sealed partial class MainPage : Page  
{  
    public int First => 1;  
    public int Second => 2;  
}  

According to the Microsoft Learn the binding should work (third example): https://learn.microsoft.com/en-us/windows/uwp/data-binding/function-bindings#path-to-the-function

Am I missing something or is this a bug in the compiler?

Both the target and the min version are set to Windows 10, version 2004.

Developer technologies Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-10-21T05:14:29.293+00:00

    Hello,
    Welcome to Microsoft Q&A,

    Invalid binding path 'sys:String.Format('{0} is now available in {0}', First,Second )' : Invalid or missmatched parameter at position '1'.

    I reproduce the compiler error, and there are two problems by testing.

    1. The type of First should be string based on the sentense “The argument types need to match the data being passed in – we don’t do narrowing conversions” in the document.
    2. Add x:Null into the String.Format() and the binding statement will work.

    For example:
    Add to the MainPage:

    <TextBlock x:Name="textBlock" Text="{x:Bind sys:String.Format(x:Null,'{0}-{1}',local:MainPage.First,local:MainPage.Second)}"/>  
    

    And the following code-behind:

    public static string First => "1";  
    public static string Second => "2";  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.