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?
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}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With