WPF MVVM Textbox Validation

前端 未结 2 473
别跟我提以往
别跟我提以往 2021-02-08 11:27

I\'m creating a WPF application using MVVM. I have a textbox, which is bound to a property in my ViewModel of type double with a default value of 0.0. If I now ente

相关标签:
2条回答
  • 2021-02-08 12:02

    You can attach a Validation Rule to the binding of the textbox that checks if the value is a valid double. This will prevent the user from being able to press the Submit button unless a valid value is entered thereby eliminating your need to check if the DoubleProperty value is valid or not upon submit because it will only be valid when the submit button is enabled. Here is a simple example:

    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
            <TextBox.Text>
                <Binding Path="DoubleProperty">
                    <Binding.ValidationRules>
                        <validationrules:NumberValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    

    In the above example you need to define a class NumberValidationRule that inherits ValidationRule.

    Here is a sample NumberValidationRule

    public class NumberValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            double result = 0.0;
            bool canConvert = double.TryParse(value as string, out result);
            return new ValidationResult(canConvert, "Not a valid double");
        }
    }
    

    Once you add a validation rule the text box will raise an error on the text field if your ValidationRule class says its not a valid value.

    To prevent the submit button from being enabled you can add a CanExecute event to it that checks if the wpf window is valid. Like so:

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Save" CanExecute="Save_CanExecute" Executed="Save_Executed"/>
    </Window.CommandBindings>
    
    ... The rest of your page
    
    <Button Content="Save" HorizontalAlignment="Left" Margin="43,146,0,0" VerticalAlignment="Top" Width="75" Command="ApplicationCommands.Save"/>                            
    

    and in the code behind

    private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = IsValid(sender as DependencyObject);
        }
    
    private bool IsValid(DependencyObject obj)
        {            
            return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid);
        }
    

    Here is a more detailed example:

    Validation in WPF

    0 讨论(0)
  • 2021-02-08 12:07

    You can try following solution. Firstly you should declare DoubleProperty as Nullable:

        public double? DoubleProperty { get; set; }
    

    Then create converter class implemented IValueConverter. It can looks like this:

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double result;
            if(!double.TryParse((string)value, out result))
            {
                return null;
            }
    
            return result;
        }
    

    Finally, you can use it:

        xmlns:converter="clr-namespace:[TestApplication]"
    
    <Window.Resources>
        <converter:DoubleToStringConverter x:Key="doubleToStringConverter" />
    </Window.Resources>
    
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding DoubleProperty, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource doubleToStringConverter}}" VerticalAlignment="Top" Width="120"/>
    

    Now, if user typed wrong values - DoubleProperty will be null.

    0 讨论(0)
提交回复
热议问题