Search code examples
xamluwpxbind

Specify signature when using x:Bind


UWP allows you to bind to a static method. I'm trying to get a time string by using

<TextBlock Text={x:Bind MyDateTime.ToString(MyPatternString)} />

where MyPatternString is "h:mm tt".

The problem is that DateTime's ToString() method has several different signatures. The first one receives an IFormatProvider. Because of this, I get a build error:

Function parameter 'provider' is invalid or missmatched

Is there any way to tell it that I wish to use the signature which accepts a string? I'd have thought it would automatically know this.


Solution

  • You can just add a method to your ViewModel and use that instead!

    That way your binding expression can be changed to this:

    <TextBlock Text={x:Bind FormatDateToString(MyDateTime)} />
    

    Be advised that this works with the Windows 10 Anniversary Update only! More info on this matter here!