WPF Data validation on Collection property in Business object
I have a business object with a property defined as such:
/// <summary>
/// Gets the Interest Payments dates Collection.
/// </summary>
public BindingList<DateTime> InterestPaymentDatesCollection
{
get
{
return this._interestPaymentDatesCollection;
}
}
This is used in a WPF app (I'm an ASP.Net developer that got handed this project) - basically I need to ensure that _interestPaymentDatesCollection
has a value set in it - otherwise I need to inform the user that the field is required, etc. Being a WPF newbie I'm unfamiliar with开发者_StackOverflow社区 how to do this. I tried reading the examples on using IDataErrorInfo but couldn't piece together how to do this on a collection property.
Advice appreciated!
Your class that holds the Collection would implement IDataErrorInfo
and you would overwrite the this[string.columnName]
property with a validation error
There are many ways to implement the validation, but here's a simple example:
public class TestModel : IDataErrorInfo
{
public List<DateTime> MyCollection {get; set;}
public string this[string columnName]
{
get { return this.GetValidationError(propertyName); }
}
public string GetValidationError(string propertyName)
{
switch (propertyName)
{
case "MyCollection":
// Do validation here and return a string if an error is found
if (MyCollection == null) return "Collection not Initialized";
}
return null;
}
}
精彩评论