2012-01-25 28 views
2

我正在嘗試執行驗證屬性。我們有一個可以爲空的屬性:如何驗證WPF中的數據並設置默認值?

public int? Number 
    { 
     get { return _number; } 
     set 
     { 
      if (_number != value) 
      { 
       _number = value; 
       RaisePropertyChanged("Number"); 
      } 
     } 
    } 

而且這個屬性綁定到一個文本框。我只想驗證這兩個方案:

  • 想象一下,用戶離開文本框爲空(textbox.text =「」),所以Number屬性必須接收空值(而不是「」)。
  • 如果用戶輸入「2b」,Number屬性必須有一個空值(因爲是錯誤),但文本框仍然必須說「2b」。

我猜IDataNotifyError和ValidationRules不適用於此。我該如何解決這些情況?

編輯:我也使用ValidationRule來顯示自定義消息,當用戶輸入不正確的格式。但是,當這種情況發生時,不會將該屬性解除爲空。如果在該錯誤中出現錯誤,它會觸發,但不會顯示任何錯誤消息。

    <TextBox.Text> 
        <Binding Path="Number" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" NotifyOnValidationError="True" Converter="{x:Static c:IntConverter.Default}" > 
         <Binding.ValidationRules> 
           <r:NumericValidation /> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox.Text> 

的驗證規則

public class NumericValidation : ValidationRule 
{ 
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 
    { 
     int? response; 
     bool noIllegalChars = TryParseStruct<int>(value.ToString(), out response); 

     if (noIllegalChars == false) 
     { 
      return new ValidationResult(false, "Input is not in a correct format."); 
     } 
     else 
     { 
      return new ValidationResult(true, null); 
     } 
    } 

    ... 
} 
+0

ValidationRules在這種情況下不起作用? –

+0

@Kai因爲驗證僅僅是爲了給用戶提供一些驗證的約束條件。我的意思是,它不像在這種情況下那樣對待邏輯。 –

回答

3

嘗試以下轉換器:

public class IntConverter : IValueConverter 
{ 
    public object Convert(object value, 
          Type targetType, 
          object parameter, 
          CultureInfo culture) 
    { 
     if (value is int?) 
     { 
      int? intValue = (int?)value; 
      if (intValue.HasValue) 
      { 
       return intValue.Value.ToString(); 
      } 
     } 

     return Binding.DoNothing; 
    } 

    public object ConvertBack(object value, 
           Type targetType, 
           object parameter, 
           CultureInfo culture) 
    { 
     if(value is string) 
     { 
      int number; 
      if (Int32.TryParse((string)value, out number)) 
      { 
       return number; 
      } 
     } 

     return null; 
    } 
} 
+0

哇,這是美麗的,我不知道轉換器。一切都還好,但我意識到當你輸入「8b」時,程序認爲沒問題,此時不更新屬性爲null。你知道如何解決它嗎? –

+0

它在「8b」的情況下設置的屬性。嘗試在轉換器的ConvertBack方法中放置斷點並查看它返回的內容。 – Amit

+0

嗯,我必須告訴你,我也在使用驗證規則。這只是在輸入不正確格式時顯示自定義消息,並且我在ValidationResults爲false時意識到,不要觸發該屬性。 –

2

您可以使用轉換器,他們被分配到基本屬性之前操作的值。

0

使用@ Amit的轉換器,然後得到錯誤使用IDataErrorInfo的用下面的代碼:

public string this[string columnName] 
{ 
    get 
    { 
     if (columnName == "Number") 
     { 
      if (Number == null) return "Invalid number"; 
     } 

     return null; 
    } 
} 

或者,您可以用您的有效性規則做,但你會需要更改其ValidationStep屬性。您的驗證目前是在默認時間RawProposedValue被觸發的,即在轉換器正在踢入之前。

+0

@Armit我不明白爲什麼Number == null是一個錯誤,我說因爲Number是一個可爲空的數據類型(int?) –

+0

Armit的轉換器會將無效數字轉換爲null。之後,IDataErrorInfo的方法將被調用來驗證。因此,空值將意味着輸入了無效的數字。 –