开发者

problems with validation rule

I am trying to get a validation rule to return an error. I implemented IDataErrorInfo in my model, which contains my business object properties and messages to return in the event validation fails. I also created a validation rule. The problem is, the validation rule is firing (bookmarked it) but the IDataErrorInfo reference in the rule never has an error, even though the IDataErrorInfo implementation of my model generates one. The datagrid definitely shows there was a validation failure.

I tested it by having the rule and model return two different messages, and the model's version is always returned. It is like my rule cannot see what is in the IDataErrorInfo object, or it is just creating a new instance of it.

DataGrid:

<DataGrid ItemsSource="{Binding Path=ProjectExpenseItemsCollection}" AutoGenerateColumns="False" 
    Name="dgProjectExpenseItems" RowStyle="{StaticResource RowStyle}" 
    SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    CanUserDeleteRows="True" CanUserAddRow开发者_JS百科s="True">
    <DataGrid.RowValidationRules>
        <vr:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
    </DataGrid.RowValidationRules>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Item Number" 
            Binding="{Binding ItemNumber, Mode=TwoWay, 
            UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
    </DataGrid.Columns>
</DataGrid>

Validation Rule:

The object "idei" isn't null, but idei.Error is always a zero-length string ("")

public class RowDataInfoValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        BindingGroup bindingGroup = (BindingGroup)value;
        IDataErrorInfo idei = bindingGroup.Items[0] as IDataErrorInfo;
        string error = (idei != null) ? idei.Error : null; 
        return (string.IsNullOrEmpty(error)) ? ValidationResult.ValidResult : new ValidationResult(false, error + ": ValidationRule");
    }
}

Model/Business Object:

public class ProjectExpenseItemsBO : IDataErrorInfo, IEditableObject, INotifyPropertyChanged
{
    public string ItemNumber { get; set; }

    public ProjectExpenseItemsBO() {}

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

    #region IDataErrorInfo Members

    public string Error
    {
        get { return this[string.Empty]; }
    }

    public string this[string propertyName]
    {
        get
        {
            string result = string.Empty;
            if (propertyName == "ItemNumber")
            {
                if (IsStringMissing(this.ItemNumber))
                {
                    result = "Item number cannot be empty-IDataError!";
                }
            }

            return result;
        }
    }

    #endregion
}


The IDataErrorInfo object that the rule gets will be an instance of your ProjectExpenseItemsBO object. The only property you check is Error, which you have implemented to return this[string.Empty], which will always return string.Empty. You probably either want to change your implementation of the Error property to look at all the errors in the object, or to have RowDataInfoValidationRule iterate through properties and get the error message for each one through the indexer.

You are getting validation errors from the model because your binding to ItemNumber has ValidatesOnDataErrors set to True, so the framework will call the indexer with the property name ItemNumber and get your error message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜