Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate the selected value in a WPF combobox

Tags:

c#

wpf

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?

like image 463
Jesus Zamora Avatar asked Jun 04 '13 17:06

Jesus Zamora


People also ask

How do I get the combobox selected value?

string strID = MyCombobox2. SelectedValue. ToString();

What is WPF data validation?

Syncfusion WPF input controls allow you to validate user input and display hints if validation fails. If the user input is invalid, a default red border will be shown around the UIElement.

What is combobox in WPF?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


1 Answers

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

like image 161
Jesus Zamora Avatar answered Sep 19 '22 15:09

Jesus Zamora