-
-
Save dani16antonio/2e4b6fd39dbbce0144ea06882dec8eed to your computer and use it in GitHub Desktop.
Xamarin Forms Currency Mask for Entry fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Text.RegularExpressions; | |
using Xamarin.Forms; | |
namespace MyProject.Util | |
{ | |
/// <summary> | |
/// Converter for using in Entry fields for masked input of currency. | |
/// <para>The binded property must be of type decimal, and must invoke the PropertyChangedEventArgs event whenever the value is changed, so that the desired mask behavior is kept.</para> | |
/// </summary> | |
public class CurrencyConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return Decimal.Parse(value.ToString()).ToString("C"); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
string valueFromString = Regex.Replace(value.ToString(), @"\D", ""); | |
if (valueFromString.Length <= 0) | |
return 0m; | |
long valueLong; | |
if (!long.TryParse(valueFromString, out valueLong)) | |
return 0m; | |
if (valueLong <= 0) | |
return 0m; | |
return valueLong / 100m; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:util="clr-namespace:MyProject.Util;assembly=MyProject" | |
x:Class="MyProject.View.SomeView" | |
Title="Some View"> | |
<ContentPage.Resources> | |
<ResourceDictionary> | |
<util:CurrencyConverter x:Key="currencyConverter" /> | |
</ResourceDictionary> | |
</ContentPage.Resources> | |
<ContentPage.Content> | |
<StackLayout Padding="20"> | |
<Entry x:Name="ValueConvert" Placeholder="Value" Text="{Binding SomeProperty, Converter={StaticResource currencyConverter}}" Keyboard="Numeric" /> | |
<!-- your fields go here --> | |
</StackLayout> | |
</ContentPage.Content> | |
</ContentPage> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a property in the ViewModel that wraps the decimal property/model you want to receive the currency value | |
public decimal SomeProperty | |
{ | |
get { | |
return YourDecimalProperty; | |
} | |
set | |
{ | |
YourDecimalProperty = value; | |
//invoke PropertyChangedEventArgs here | |
//... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment