繁体   English   中英

验证WPF组合框中的选定值

[英]Validate the selected value in a WPF combobox

就像标题所说的那样,我正在尝试验证我的表单,但是我在获取组合框值时遇到了问题:

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

然后使用我的类FormValidation进行验证,如下所示:

            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.";
               }

            }

我使用这些函数在我的表单中调用验证。

    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--;
    }

我希望得到所选的值,而不是所选的文本。 什么是我最好的选择?

对我来说,愚蠢,愚蠢的缺乏观察力。 如果您要将ComboBox的Text更改为SelectedValue ,请执行以下操作:

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

一个将获得所选值而不是文本。

对于那些可能想要阅读的人,我在这里找到了原始教程。

使用IDataErrorInfo进行WPF TextBox验证

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM