How can i add a format for a binding, that formats the bound value with string.Format or something similar? I saw in other threads, that you can pass a converterName.
I browsed the code shortly, but couldn't find something. I know that there could happen information lost which destroys two way binding, but i only want this for displaying values. My concrete case is Binding of a DateTime.
bindings.Bind(purchaseDate).To(vm => vm.RegisteredDevice.PurchaseDate);
my wish e.g.:
bindings.Bind(purchaseDate).To(vm => vm.RegisteredDevice.PurchaseDate).WithFormat("hh:mm");
To do this, you can just create a StringFormatValueConverter and you can use it's parameter as the format string to use.
Should take about 2 minutes to write... here, I'll prove it:
public class StringFormatValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
if (parameter == null)
return value;
var format = "{0:" + parameter.ToString() + "}";
return string.Format(format, value);
}
}
then
set.Bind(myLabel).To(vm => vm.TheDate).WithConversion("StringFormat", "HH:MM:ss");
1 minute 53 seconds ;)
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