简体   繁体   中英

TextBox validation error

I am using the custom TextBox and it takes Double value on which i have applied some validations, it works fine but when I press BackSpace it also removes the decimal point which annoys. Forexample if current value is "2.5" and I press backspace ".5" is removed whereas the desired value should be "2."

Here is the xaml of my custom TextBox which is used in a UserControl

<rmguiutil:RMTextBox Margin="5,5,0,0" Width="40" HorizontalAlignment="Left" OnlyAllow="Double"
    Text="{Binding StartConcentration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
    IsEnabled="{Binding IngredientIngredientTypeRow, Converter={StaticResource GlobalNullObjectToBooleanConverter}, FallbackValue=False}" />

Here is the code behind for my custom TextBox, I have overridden its PreviewTextInput event

protected override void OnPreviewTextInput( System.Windows.Input.TextCompositionEventArgs e )
{
    base.OnPreviewTextInput( e );

    if( OnlyAllow == RMTextBoxOnlyAllow.Double && ( e.Text.Any( c => !Char.IsDigit( c ) && c != '.' ) || ( e.Text.Count( c => c == '.' ) + Text.Count( c => c == '.' ) ) > 1 ) )
    e.Handled = true;
    else if( OnlyAllow == RMTextBoxOnlyAllow.Integer && e.Text.Any( c => !Char.IsDigit( c ) ) )
    e.Handled = true;
}

I could not find any solution regarding my problem.

UpdateSourceTrigger of Binding StartConcentration is set to PropertyChanged , which means it'll validate every time a change is made to the value. LostFocus would be better.

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