Master
Master

Reputation: 2153

TextBox Won't Allow me to enter in a decimal

I can Enter Numbers into the text box but it won't allow me to enter decimal values. May I ask why is this?

<TextBox Text="{Binding SebAmountPer, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2" TextWrapping="Wrap"/>

public decimal? SebAmountPer
{
    get
    {
        return _seb.SEBAmountPer;
    }
    set
    {
        _seb.SEBAmountPer = value; 
        OnPropertyChanged("SebAmountPer");
        OnPropertyChanged("SebTotal");
    }
}

Upvotes: 4

Views: 7239

Answers (8)

Daniel Mata
Daniel Mata

Reputation: 1

try this one. I changed the

UpdateSourceTrigger=PropertyChanged 

to

UpdateSourceTrigger=LostFocus

<TextBox Text="{Binding SebAmountPer, UpdateSourceTrigger=LostFocus}" Grid.Column="1" Grid.Row="2" TextWrapping="Wrap"/>

Upvotes: 0

Salvador Nasser
Salvador Nasser

Reputation: 79

You can change from UpdateSourceTrigger=PropertyChanged to UpdateSourceTrigger=LostFocus. It will allows you insert dot/comma and it validates on cursor box change.

Upvotes: 0

Gabriel Worley
Gabriel Worley

Reputation: 66

I used a Value Converter to solve this problem.

public class DecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            string s_value = value.ToString();
            if (s_value.EndsWith("."))
            {
                return s_value.Replace(".", ".0");
            }
            else if (s_value.Contains(".") && s_value.EndsWith("0"))
            {
                return s_value.Substring(0, s_value.Length - 1);
            }
            return value;
        }
        return null;
    }
}



<TextBox>
    <Binding Path="UnitCost" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource decimalConverter}"/>
</TextBox>

Upvotes: 1

Heisenberg
Heisenberg

Reputation: 776

It is binding value validation issue, you just need to set

UpdateSourceTrigger=LostFocus

and in the TextBox_PreviewTextInput use 123 456 789 0's answer:

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
   Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
   e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text));
}

Upvotes: 3

Slaven Tojić
Slaven Tojić

Reputation: 3014

If you have .NET 4.5 or newer add to the App.xaml.cs file System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; like shown below:

public partial class App : Application
{
    public App()
    {
        System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
    }
}

Upvotes: 2

d.moncada
d.moncada

Reputation: 17402

A cheap work-around (if you still want to keep the built-in validation and binding to a nullable property), is to add a small Delay within the binding. This allows you to actually enter in a 'decimal' point, and after the 'delay' it binds, then evaluates the value as correct.

Example:

  <TextBox Text="{Binding SebAmountPer, UpdateSourceTrigger=PropertyChanged, TargetNullValue='', Delay=350}" Height="75" Width="300" TextWrapping="Wrap"/>

Upvotes: 5

123 456 789 0
123 456 789 0

Reputation: 10865

This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.

Following are valid digits: 1, 3.3, 23.124, 1., 23.0

Following are invalid digit: . -->(i meant dot), .12

so use the following code if you don't want to do that.

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
   Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
   e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text));
}

Upvotes: 0

ZoolWay
ZoolWay

Reputation: 5505

You have a two-way binding to a numeric datatype. The trigger is property change which means after every key stroke. Try binding to a string first or change update trigger.

You enter "2." and he updates the binding to 2.0 and converts it back and just steals away your dot ;)

Upvotes: 6

Related Questions