【问题标题】:xaml validation error rad line and error tooltip not disply using IDataErrorInfo?使用 IDataErrorInfo 不显示 xaml 验证错误红线和错误工具提示?
【发布时间】:2012-11-07 05:02:29
【问题描述】:

在我的项目中,我使用了 IDataErrorInfor 进行验证,在 xaml 代码中我提到了文本框的 NotifyfyOvalidationError= true。

所有验证都正确执行,但唯一的问题是,在我看来,它没有将错误模板显示为跨 TextBox 和工具提示的红线,我想显示它以观察此 TextBox 包含错误..

同样的想法适用于所有其他文本框,另一件事是,无论我在代码中设置什么,这个文本框的验证都是从视图模型中工作的。

Xaml:

 <TextBox  Margin="0,7"  Text="{Binding Path=Address.AddressLines, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Stretch" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"  /> 
       

查看模型:

区域 IDataErrorInfo 成员

string IDataErrorInfo.Error { get { return null; } }

string IDataErrorInfo.this[string propertyName]
{
    get { return this.GetValidationError(propertyName); }
}

#endregion // IDataErrorInfo Members

#region Validation

/// <summary>
/// Returns true if this object has no validation errors.
/// </summary>
public bool IsValid
{
    get
    {
        foreach (string property in ValidatedProperties)
            if (GetValidationError(property) != null)
                return false;

        return true;
    }
}

static readonly string[] ValidatedProperties = 
{ 
      "Address",
};
string GetValidationError(string propertyName)
{
    if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
        return null;

    string error = null;

    switch (propertyName)
    {
      
        case "Address":
            error = this.ValidateAddressLine();
            break;
           
        default:
            Debug.Fail("Unexpected property being validated on School: " + propertyName);
            break;
    }

    return error;
}
string ValidateAddressLine()
{
    if (IsStringMissing(this.Address.AddressLines))
    {
        return "Enter Address.";
    }
    return null;
}

static bool IsStringMissing(string value)
{
    return
        String.IsNullOrEmpty(value) ||
        value.Trim() == String.Empty;
}
#endregion // Validation

任何人都可以解决我的问题..

【问题讨论】:

    标签: wpf validation xaml mvvm idataerrorinfo


    【解决方案1】:
          public string Error
        {
            get { return null; }
        }
    
        public string this[string columnName]
        {
            get
            {
                var error = string.Empty;
                switch (columnName)
                {
                    case "Address.AddressLines":
                        if (string.IsNullOrEmpty(Address.AddressLines))
                            error = "Address.AddressLines Required";
                        break;
    
    
                }
                return error;
            }
        }
    

    使用此代码

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多