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 窗口的 CodeBehind 具有以下功能:

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

然而由于某种原因,我不会看到事件触发。如果我在 ValidationResult 中标记断点,它不会做任何事情。

4

1 回答 1

1

您的 ValidationRuleRequiredFields也用作但未声明DataContext该属性FirstName。所以绑定实际上是失败的。你应该定义一个单独的 ViewModel,如果你仍然想使用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。

于 2014-10-27T02:58:17.513 回答