简体   繁体   中英

Validate the selected value in a WPF combobox

Like the title says, I am trying to validate my form, but I have an issue getting the combobox value:

<ComboBox Name="ComboBox_Country" 
          Validation.Error="Validation_Error"
          Text="{Binding UpdateSourceTrigger=PropertyChanged, 
                         Path=Form_Country,
                         ValidatesOnDataErrors=true,
                         NotifyOnValidationError=true}"/>

Which is then validated with my class FormValidation like so:

            public string this[string columnName]
            {
               get
            {
               string result = null;

               if (columnName == "Form_Country")
               {
                  if (string.IsNullOrEmpty(Form_Country) || !verifyNumericValue(Form_Country))
                    result = "Please choose a correct option.";
               }

            }

I use these functions to call the validation in my form.

    private void Confirm_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = _errors == 0;
        e.Handled = true;
    }

    private void Confirm_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        _generic = new UnidadValidation();
        grid_UnidadData.DataContext = _generic;
        e.Handled = true;
    }

    private void Validation_Error(object sender, ValidationErrorEventArgs e)
    {
        if (e.Action == ValidationErrorEventAction.Added)
            _errors++;
        else
            _errors--;
    }

I wish to get the selected value, not the selected text. What is my best option?

Stupid, stupid stupid lack of observation on my part. If you were to change the ComboBox's Text to SelectedValue instead like so:

<ComboBox Name="ComboBox_Pais" 
          Validation.Error="Validation_Error"
          SelectedValue="{Binding UpdateSourceTrigger=PropertyChanged, 
                                  Path=Escuela_Pais,
                                  ValidatesOnDataErrors=true, 
                                  NotifyOnValidationError=true}"/>

One will get the selected value and NOT the text.

For those of you who might want to read on it, I found the original tutorial here.

WPF TextBox Validation with IDataErrorInfo

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