0

I have 2 TextBoxes in the window binded with appropriate properties in C# code. One of them is int, so default INotifyPropertyChanged integer validation works great, but the second one is double, but I still can't insert any charachter except a number. How can I change default in validation type? XAML:

<TextBox Grid.Row="3"
                     Grid.Column="1"
                     x:Name="priceTextBox"
                     Width="250"
                     Height="30"
                     HorizontalAlignment="Left"
                     Text="{Binding Price, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBox Grid.Row="4"
                     Grid.Column="1"
                     x:Name="soldCountTextBox"
                     Width="250"
                     Height="30"
                     HorizontalAlignment="Left"
                     Text="{Binding SoldCount, UpdateSourceTrigger=PropertyChanged}"/>

C#:

public double Price
    {
        get => _price;
        set
        {
            _price = value;
            OnPropertyChanged("Price");
        }
    }

    public int SoldCount
    {
        get => _soldCount;
        set
        {
            _soldCount = value;
            OnPropertyChanged("SoldCount");
        }
    }
  • https://stackoverflow.com/questions/49432472/textbox-updates-itself-while-typing, https://stackoverflow.com/questions/26162784/wpf-updatesourcetrigger-propertychanged-issue-with-double-values – ASh Oct 21 '22 at 07:51

1 Answers1

-1

When you are working with Numbers or Currency, I suggest you use Xceed controls

xmlns:Xceed="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"

        <Xceed:IntegerUpDown Grid.Row="3"
                 Grid.Column="1"
                 x:Name="priceTextBox"
                 Width="250"
                 Height="30"
                 HorizontalAlignment="Left"
                 Value="{Binding Price, UpdateSourceTrigger=PropertyChanged}"/>
        <Xceed:DecimalUpDown Grid.Row="4"
                 Grid.Column="1"
                 x:Name="soldCountTextBox"
                 Width="250"
                 Height="30"
                 HorizontalAlignment="Left"
                 Value="{Binding SoldCount, UpdateSourceTrigger=PropertyChanged}"/>
  • Adding a whole 3rd party library for this one feature doesn't seem like a good solution imo. – Felix Castor Nov 03 '22 at 15:08
  • Felix Castor I appreciate your feedback, I'm a programmer for 20years, I suggest him to use that library because default visual studio libraryes are basic, and you have to spend a lot of time and codes to reach what is already included in Xceed library (free version) – Mustafa Mutasim Nov 04 '22 at 16:22