2016-07-31 3 views
0

私は、ユーザの名前をtextBoxに入力する必要があるアプリケーションを作成しています。私はこれを行うためのチュートリアルを休んだ。しかし、ボタンをクリックしてtextBoxを検証したところ、名前を入力していないと、「名前を入力する必要があります」という妥当性検査エラーが表示されません。代わりに、textBoxにテキストを入力してからテキストを削除し、ボタンをクリックしてエラーメッセージを表示する必要があります。これは私がOnProperyChangedメソッドを使って行っているからです。 textBoxのテキストを最初に入力してからテキストを削除することなく検証できる方法はありますか?テキストボックスの検証

XAML

<TextBox.Text> 
     <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus"> 
      <Binding.ValidationRules> 
       <local:NameValidator></local:NameValidator> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

NameValidator.cs

public class NameValidator : ValidationRule 
{ 
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 
    { 
     if (value.ToString().Length ==0) 
      return new ValidationResult(false, "value cannot be empty."); 
     else 
     { 
      if (value.ToString().Length > 3) 
       return new ValidationResult(false, "Name cannot be more than 3 characters long."); 
     } 
     return ValidationResult.ValidResult; 
    } 
} 

xaml.cs

if (!Validation.GetHasError(tbxName)) 
      { 
       // do the proicessing 
      } 


private void OnPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 

答えて

0

テキストの検証を行うには非常に単純な方法を休閑として

私のコードですボックスはValidatonを使用していますルールのテクニック:

検証例を支配:

public class NumericValidationRule : ValidationRule 
    { 
     public Type ValidationType { get; set; } 
     public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
     { 
      string strValue = Convert.ToString(value); 

      if (string.IsNullOrEmpty(strValue)) 
       return new ValidationResult(false, $"Value cannot be coverted to string."); 
      bool canConvert = false; 
      switch (ValidationType.Name) 
      { 

       case "Boolean": 
        bool boolVal = false; 
        canConvert = bool.TryParse(strValue, out boolVal); 
        return canConvert ? new ValidationResult(true, null) : new ValidationResult(false, $"Input should be type of boolean"); 
       case "Int32": 
        int intVal = 0; 
        canConvert = int.TryParse(strValue, out intVal); 
        return canConvert ? new ValidationResult(true, null) : new ValidationResult(false, $"Input should be type of Int32"); 
       case "Double": 
        double doubleVal = 0; 
        canConvert = double.TryParse(strValue, out doubleVal); 
        return canConvert ? new ValidationResult(true, null) : new ValidationResult(false, $"Input should be type of Double"); 
       case "Int64": 
        long longVal = 0; 
        canConvert = long.TryParse(strValue, out longVal); 
        return canConvert ? new ValidationResult(true, null) : new ValidationResult(false, $"Input should be type of Int64"); 
       default: 
        throw new InvalidCastException($"{ValidationType.Name} is not supported"); 
      } 
     } 
    } 

XAML:

非常に重要:ValidatesOnTargetUpdated = "true" のそれは、この定義せずに動作しません設定することを忘れないでください。

<TextBox x:Name="Int32Holder" 
           IsReadOnly="{Binding IsChecked,ElementName=CheckBoxEditModeController,Converter={converters:BooleanInvertConverter}}" 
           Style="{StaticResource ValidationAwareTextBoxStyle}" 
           VerticalAlignment="Center"> 
          <!--Text="{Binding Converter={cnv:TypeConverter}, ConverterParameter='Int32', Path=ValueToEdit.Value, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"--> 
          <TextBox.Text> 
           <Binding Path="Name" 
             Mode="TwoWay" 
             UpdateSourceTrigger="PropertyChanged" 
             Converter="{cnv:TypeConverter}" 
             ConverterParameter="Int32" 
             ValidatesOnNotifyDataErrors="True" 
             ValidatesOnDataErrors="True" 
             NotifyOnValidationError="True"> 
            <Binding.ValidationRules> 
             <validationRules:NumericValidationRule ValidationType="{x:Type system:Int32}" 
                       ValidatesOnTargetUpdated="True" /> 
            </Binding.ValidationRules> 
           </Binding> 
          </TextBox.Text> 
          <!--NumericValidationRule--> 
         </TextBox> 
関連する問題