2014-10-26 53 views
0

我遇到了验证问题,至今一直是一场真正的斗争。我改变了一些代码,并阅读了很多关于这个,并遵循本指南大部分的道路:http://developingfor.net/2009/10/13/using-custom-validation-rules-in-wpf/,但我有问题。验证没有解雇,我找不到原因!我会发布一些我的代码。验证规则无法解雇

public class RequiredFields : ValidationRule 
    { 
     private String _errorMessage = String.Empty; 
     public string ErrorMessage 
     { 
      get { return _errorMessage; } 
      set { _errorMessage = value; } 
     } 

     public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
     { 
      var str = value as string; 

      if (String.IsNullOrEmpty(str)) 
      { 
       return new ValidationResult(false, this.ErrorMessage); 
      } 

      return new ValidationResult(true, null); 
     } 
    } 

XAML:

 <Style 
x:Key="textBoxInError" 
TargetType="{x:Type TextBox}"> 
     <Style.Triggers> 
      <Trigger 
     Property="Validation.HasError" 
     Value="true"> 
       <Setter 
      Property="ToolTip" 
      Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> 
       <Setter 
      Property="Background" 
      Value="Red" /> 
       <Setter 
      Property="Foreground" 
      Value="White" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

文本框XAML:

<TextBox x:Name="txtFirstName" Validation.ErrorTemplate="{StaticResource validationTemplate}" HorizontalAlignment="Left" Height="23" Margin="156,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149"> 
     <TextBox.Text> 
      <Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"> 
       <Binding.ValidationRules> 
        <validators:RequiredFields ErrorMessage="Name is Required" /> 
       </Binding.ValidationRules> 
      </Binding> 
     </TextBox.Text> 
    </TextBox> 

代码隐藏在XAML窗口都具有此:

 RequiredFields ss = new RequiredFields(); 
     this.DataContext = ss; 

但出于某种原因,我不会看到事件发生。如果我在ValidationResult中标记一个断点,它将不会执行任何操作。

回答

1

您的验证规则RequiredFields也用作DataContext,但未声明属性FirstName。所以绑定实际上失败了。你应该定义一个单独的视图模型,在情况下,如果你仍然想使用RequiredFields作为DataContext的,你必须添加属性FirstName这样的:

public class RequiredFields : ValidationRule, INotifyPropertyChanged 
{ 
    private String _errorMessage = String.Empty; 
    public string ErrorMessage 
    { 
     get { return _errorMessage; } 
     set { _errorMessage = value; } 
    } 

    public override ValidationResult Validate(object value, 
               CultureInfo cultureInfo) 
    { 
     var str = value as string; 

     if (String.IsNullOrEmpty(str)) 
     { 
      return new ValidationResult(false, this.ErrorMessage); 
     } 

     return new ValidationResult(true, null); 
    } 
    //Implements INotifyPropertyChanged 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string prop){ 
     var handler = PropertyChanged; 
     if(handler != null) handler(this, new PropertyChangedEventArgs(prop)); 
    } 
    //add the property FirstName 
    string _firstName; 
    public string FirstName { 
     get { 
      return _firstName; 
     } 
     set { 
      if(_firstName != value) { 
      _firstName = value; 
      OnPropertyChanged("FirstName"); 
      } 
     } 
    } 
} 

上面的代码只是一个权宜之计和示范解决方案,而比实际做法要好。你应该如创建一些基类实现INotifyPropertyChanged并实现单独 ViewModel而不是使用一些现有的ValidationRule。

+1

感谢您的答案,我有互联网问题,但我会验证这一点。感谢国王:) – 2014-10-31 22:48:24