Search code examples
c#wpfvalidationidataerrorinfo

Please help me simplify this Data Validation (IDataErrorInfo)


I have a bunch of textboxes in my application that all have to be filled with numbers (doubles). I want to use Data Validation to make sure this happens and that a user doesnt enter strings. Currently in my XAML I have:

<TextBox x:Name="VoorzieningBerging"
         Text="{Binding Path=VoorzieningBerging, Source={StaticResource Gegevens},
         ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,
         FallbackValue=0}"/>

And in my Resource I have the following class:

public class GegevensValidatie : IDataErrorInfo
{
    public string VoorzieningBerging
    {
        get;
        set;
    }

    public string Error
    {
        get { return null; }
    }

    public string this[string name]
    {
        get
        {
            string err = string.Empty;

            if (name == "VoorzieningBerging") {
                try
                {
                    double.Parse(VoorzieningBerging);
                }
                catch
                {
                    err = "error";
                }
            }

            return err;
        }
    }
}

However after a lot of messing around trying to get other DataValidations to work this has stopped working for me also. Please help me find an efficient way to check if all my textboxes have doubles for value.

Edit, when changing the DataBinding to:

Mode=OneWayToSource

It will work and show "error" when I fill in anything but a number in the textbox. But still, adding an "if" statement for every single textbox in my application is going to be very inefficient


Solution

  • Just for here above

    if (name == "VoorzieningBerging") 
            error = double.TryParse(val.ToString(), out result) ? null : "error";
    

    If you want to suppress inputting double value you can handle PreviewTextInput event where you set object of EventArgs(e.Handled) to true if double came along.

     private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            double result;
            if (double.TryParse(((TextBox)sender).Text, out result))
                e.Handled = true;
        }
    

    For many check this out

      <TextBox>
            <TextBox.Text>
                <Binding Path="Name" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:Validation2/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    
    public class Validation2 : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            double result;
            return double.TryParse(value.ToString(), out result) == true ? new ValidationResult(true, null) : new ValidationResult(false, "error");
        }
    }
    

    Comprehensive information may be found here