IDateErrorInfo and Validation

Grids for WPF Forum

Posted 10 years ago by Rick Edwards - UK
Version: 9.1.0502
Avatar
Hi all,

I seem to be having problems trying to get the PropertyGrid to recognise the fact that my selected objects implement IDataErrorInfo and wanted to make sure I've not missed anything. Basically I have a main window that is set as the DataContext for my application and the PropertyGrid SelectedObjects field is data bound to a SelectedObjects field in the main window that returns an observable collection of objects. Now I add an object to my collection and sure enough the PropertyGrid reflects over it correctly. I then implement IDataErrorInfo on my object with the folowing:

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

string IDataErrorInfo.this[string columnName]
{
  get
  {
    ...
  }
}

If I add a break point to the getter in the second method it is never called. I've been through the demo app and implemented the Validation.Error="OnPropertyGridValidationError" event handler just to prove that validation works and sure enough if I edit a property and enter a value of the wrong type (string instead of int ca.) then I view the correct error message in the list view.

Am I missing something in the PropertyGrid to enable the IDataErrorInfo validation, it's not clear from the demo code?

Thanks

Rick

Comments (4)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Rick,

Can you please send over a small sample that shows the issue to our support address?


Actipro Software Support

Posted 10 years ago by Rick Edwards - UK
Avatar
Thanks, have sent some demo code over. Seems I'm having problems implementing the IDateaErrorInfo interface correctly in conjunction with my custom property data accessor (maybe?).

Regards

Rick
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Rick,

You are correct. Our implementations of IPropertyDataAccessor also implement IDataErrorInfo, where calls to the latter are forwarded to the underlying object (i.e. Customer and Address) if it implements IDataErrorInfo.

I didn't see in your sample where Customer implements IDataErrorInfo, but I believe you need to implement IDataErrorInfo in your CustomDataPropertyAccessor. The code would be:
/// <summary>
/// Gets an error message indicating what is wrong with this object.
/// </summary>
/// <value></value>
/// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
string IDataErrorInfo.Error {
    get {
        ITypeDescriptorContext context = this.dataAccessor as ITypeDescriptorContext;
        if (null != context) {
            IDataErrorInfo errorInfo = context.Instance as IDataErrorInfo;
            if (null != errorInfo)
                return errorInfo.Error;
        }
        return null;
    }
}

/// <summary>
/// Gets the <see cref="String"/> with the specified column name.
/// </summary>
/// <value></value>
string IDataErrorInfo.this[string columnName] {
    get {
        ITypeDescriptorContext context = this.dataAccessor as ITypeDescriptorContext;
        if (null != context) {
            if ("Value" == columnName || "ValueAsString" == columnName) {
                IDataErrorInfo errorInfo = context.Instance as IDataErrorInfo;
                if (null != errorInfo)
                    return errorInfo[this.ValueName];
            }
        }
        return null;
    }
}
After you add that, you should see calls into your Customer object.


Actipro Software Support

Posted 10 years ago by Rick Edwards - UK
Avatar
Excellent, thanks for that, was the missing piece in my code!

It looks like I should be able to use the Validation Application Block with IDataErrorInfo with the PropertyGrid now. I'll update my other forum post concerning the VAB once I've tidied up the loose ends.

Thanks again for all your help in answering my queries, has been absolutely invaluable in my evaluation. Very happy now.

Regards

Rick
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.