简体   繁体   中英

How to force a TextBox in windows phone 8 to allow a decimal number with two digits after point

I've been looking for some clue to force through some event textbox that only allows me to enter numbers and go building a decimal number only 2 digits after the point, something like jQuery.MaskMoney in development with jQuery because in windows phone need to collect this information. But so far I've found what I've tried to replicate in my code, without success. Regular expressions help but the idea is to force the user but not show the validation message that would validate my regular expression. I understand that development in Windows Phone 8 is a bit limited in terms of resources so I assume that what is needed is a custom.

<TextBox Name="TxtAmountValue" InputScope="Number" Style="{StaticResource MyTextBoxStyle}" MaxLength="5" />

For example if the amount to be paid is 12.52 what should be appearing is: 0.01 -> 0.12 -> 1.25 up to the 12.52

Update

decimal value;
            if (decimal.TryParse(control.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
            {
                if (e.Key == Key.Back && str.Length > 0)
                {
                    str = (value / 10).ToString("#0.00");
                }
                else
                {
                    if (str.Length == 1 && value == 0)
                    {
                        str = value.ToString("#0.00", CultureInfo.InvariantCulture);
                    }
                    else if (str.Length >= 4 && str.Length <= 5)
                    {
                        decimal tempValue = value;
                        str = (value * 10).ToString("#0.00");
                        if (str.Length > 5)
                        {
                            str = tempValue.ToString("#0.00", CultureInfo.InvariantCulture);
                        }
                    }
                    else
                    {
                        str = (value / 100).ToString("#0.00");
                    }
                }
                control.Text = str;
                control.SelectionStart = control.Text.Length;
            }

for this you should be using the mvvm light toolkit/architecture.

the textbox is then bound to a property.

The property can then have a data annotation which can be a regular expression that allows only such a text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM