Last active
July 13, 2023 06:01
-
-
Save alfeugds/786260ab70a49565070338052ceaee3e 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 | |
//... | |
} | |
} |
devonuto
commented
Oct 10, 2021
•
I have the same problem :
I have a problem about formatting. I tryed all of sample project . When I want to delete amount , comma is not skipped. For Example , entry amount default : 0,00
I entry 12.345,34 . I pressed backspace key for once. Amount: 1.234,53 , Normally I would expect to set as 12.345,30.
Also When I started to write amount, 0,00 amount isn't deleted. For Example I want to write "3". Amount set as 30,00 .
I can't find where I'm doing wrong. Could you help me with that please ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment