PropertyGridItem validation using IDataErrorInfo

Grids for WPF Forum

Posted 9 years ago by Ray Huger
Version: 10.1.0522
Avatar
The Actipro Property Grid examples make use of error validation, however the QuickStart.DataValidation example relies on annotation to create the PropertyGridPropertyItems and is more complex than what I need for my project.

I have a PropertyGrid that is databound to one of my classes which implements IDataErrorInfo, and when databound to a normal WPF element (such as a textbox), validates correctly (displaying the default red box and my custom tooltip). However, I am not able to reproduce this behavior using PropertyGrid and PropertyGridItem. It seems that it is attempting to validate, but no visual decoration occurs. How does one do this?

Comments (8)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Ray,

This forum post discusses the issue you are seeing.

But basically if you are using PropertyGridPropertyItem, then you won't be able to leverage the data validation code.


Actipro Software Support

Posted 9 years ago by Ray Huger
Avatar
If I use the [Description] and [Category] annotations I don't know how to implement them for languages in addition to English. With PropertyGridPropertyItem I can use a resource file. Is there some XAML-only way of doing this without using PropertyGridPropertyItems?

Also in the other post you refer to, the validation that failed was based on Exceptions. Mine is based on IDataErrorInfo. Are both methods of validation unavailable?
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Ray,

Unfortunately yes, data validation doesn't work even if you are using IDataErrorInfo. The Binding between your data object and PropertyGridPropertyItem.Value will check the IDataErrorInfo on your object, and may or may not be marked as invalid. The Binding between PropertyGridPropertyItem.Value and TextBox.Text (or any other control) doesn't even know about your data object, so it doesn't have an IDataErrorInfo to query. The PropertyGridPropertyItem can't implement and forward IDataErrorInfo calls, because it doesn't know about your data object.

You could probably create a derivation of PropertyGridPropertyItem that implements IDataErrorInfo and exposes a new DependencyProperty (say DataErrorInfo of type IDataErrorInfo). Then in the PropertyGridPropertyItem implementation of the IDataErrorInfo members, you'd forward the calls to the specified DataErrorInfo. In your XAML, you'd need to set both Value and the DataErrorInfo.


Actipro Software Support

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sorry, missed the first paragraph :-)

You can resource files with DescriptionAttribute and CategoryAttribute as well. This blog post explains the process.

[Modified at 06/16/2010 02:57 PM]


Actipro Software Support

Posted 9 years ago by Ray Huger
Avatar
Regarding my question:

Is there some XAML-only way of doing this without using PropertyGridPropertyItems?

I assume the answer is NO.

So to do error handling I need to use the [attribute] method instead of the PropertyGridPropertyItem method. And to achieve localization I have to subclass System.ComponentModel setting up [MyDescription] and [MyCategory] attributes.
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Ray,

That is correct, you cannot currently use PropertyDescriptorDataAccessor (which you need to use for IDataErrorInfo support) directly from XAML. I've marked down a TODO item to try to find a way to use PropertyDescriptorDataAccessor from XAML.

Your last sentence is a bit misleading. To support IDataErrorInfo, you can't use PropertyGridPropertyItem (since there are actually two bindings). You have to use something like PropertyDescriptorDataAccessor, or your own custom implementation of IPropertyDataAccessor.

To support localization, you can use the method described in the blog above (i.e. creating a derivation of CategoryAttribute or DescriptionAttribute) or you could create a derivation of PropertyDescriptorDataAccessor, or your own custom implementation of IPropertyDataAccessor. The IPropertyDataAccessor interface, which PropertyDescriptorDataAccessor implements, exposes two properties: Category and Description. So you could create a derivation of PropertyDescriptorDataAccessor, whose constructor takes two additional parameters that specify the resource string to use for the category and description. Then you'd override the CategoryInternal and DescriptionInternal properties and return the appropriate resource string.

So basically, if you want to use PropertyDescriptorDataAccessor as-is, then you'd have to use CategoryAttribute/DescriptionAttribute (or a localizable version). If you create a custom PropertyDescriptorDataAccessor, then you have alot more flexibility.


Actipro Software Support

Posted 9 years ago by Ray Huger
Avatar
Is there an example of using PropertyDescriptorDataAccessor? The Sample Browser does not appear to use it.
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Ray,

We don't currently have a sample that uses PropertyDescriptorDataAccessor, but it's basically:
// If A is a property on myObject, you can access like so
PropertyDescriptorDataAccessor property = new 
PropertyDescriptorDataAccessor(myObject, "A");

// Create a category to hold property, or you could add property directly to the PropertyGrid
CategoryDataAccessor category = new CategoryDataAccessor("My Category"); 
category.Accessors.Add(property); 

this.propertyGrid.Items.Add(category);
You can then add any number of CategoryDataAccessor and/or PropertyDescriptorDataAccessor you like. You can even add a CategoryDataAccessor to another CategoryDataAccessor to create nested categories.

[Modified at 06/18/2010 09:06 AM]


Actipro Software Support

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.