Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvvMCross Binding with format string

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.

  • Does a converter for this issue exists?
  • Where can i see a list of the standard converters of MvvMCross v3?

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");
like image 663
Sven-Michael Stübe Avatar asked May 06 '13 12:05

Sven-Michael Stübe


1 Answers

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 ;)

like image 151
Stuart Avatar answered Oct 01 '22 22:10

Stuart