4

I am using a Label in Xamarin.Forms in code like so:

var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Time", stringFormat: "{}{0:hh\\:mm}", mode: BindingMode.TwoWay, source: this));

But this returns the error:

System.FormatException: Input string was not in a correct format.

But this works in Xaml:

 <Label Text="{Binding StartTime, StringFormat='{}{0:hh\\:mm}'}}"/>

How do I use string format on a binding for TimeSpan in Xamarin.Forms?

JKennedy
  • 18,150
  • 17
  • 114
  • 198

2 Answers2

4

Xamarin Forms uses all the basic string.Format options you would normally use. So for a datetime the stringFormat variable would look like this:

"{0:MM/dd/yy H:mm:ss zzz}"

The additional pair of brackets in your format string seem out of place to me. You could try the following for what you're trying to achieve here:

@"{0:hh\:mm}"
Steven Thewissen
  • 2,971
  • 17
  • 23
0

StringFormat='{}{0:hh\\:mm\\:ss}'

Cananau Cristian
  • 436
  • 2
  • 6
  • 30
  • Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Feb 06 '21 at 05:50