Binding Data in MVVM

RichardGoverRichardGover GBUniversity
edited March 2016 in Xamarin.Forms

When I bind a number to a Text Entry control in Xaml and use String Format or IValueConverter

Entry Text="{Binding ItemGross, Mode=TwoWay, Converter={StaticResource currencyCnv }}" FontSize="10" VerticalOptions="End" Grid.Column="1" Grid.Row="1" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End" Keyboard="Keyboard.Numeric" WidthRequest="100"

The value is not passed back.

while

Entry Text="{Binding ItemGross, Mode=TwoWay}}" FontSize="10" VerticalOptions="End" Grid.Column="1" Grid.Row="1" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End" Keyboard="Keyboard.Numeric" WidthRequest="100"

does not

Please help

Posts

  • RichardGoverRichardGover GBUniversity
    edited March 2016

    I am displaying currency

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    var currencyCnv = value.ToString();
    string newValue = Format("{0:£###,##0.00}", value);
    return newValue;
    }

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @RichardGover

    We need more information than this to help.

    Are you sure the ValueConverter is been called?

    Here's a quick example

    {Binding BooleanField, Converter={StaticResource BoolToString}, StringFormat='Boolean Value: {0}'}

    In your page resources

    <ContentPage.Resources>
            <ResourceDictionary>
                <convert:BooleanToStringConverter x:Key="BoolToString" />
            </ResourceDictionary>
        </ContentPage.Resources>
    
    public class BooleanToStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (!(value is bool)) return "No";
                return (bool)value ? "Yes" : "No";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException("Only one way bindings are supported with this converter");
            }
        }
    
  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @RichardGover

    You can format your bindings in XAML btw.

    Text="{Binding Order.OrderData,StringFormat='{0:MMMM dd, yyyy @ HH:mm}'}" />

  • Maharshi.5212Maharshi.5212 USMember ✭✭

    public static class Constants
    {
    public static CultureInfo hindi = new CultureInfo("hi-IN");
    public static CurrencyConverter converter = new CurrencyConverter();
    }

    public class CurrencyConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return String.Format(Constants.hindi, "{0:C}", value);
    }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    In your xaml

    ResourceDictionary

      c:CurrencyConverter x:Key="RupeeConverter"
    

    /ResourceDictionary

    Label Grid.Column="5" Text="{Binding Discount, Converter={StaticResource RupeeConverter} }" VerticalOptions="Center" TextColor="{Binding DiscountTextColor}" BackgroundColor="{Binding DiscountBackgroundColor}" FontSize="14" HorizontalOptions="Center"

  • Maharshi.5212Maharshi.5212 USMember ✭✭

    The above code is for displaying currency you can use it for your other scenarios

Sign In or Register to comment.