2) How to mix telerik Attributes with Microsoft Attributes with Property Grid?
I would like to be MVVM conform !
Example of Validation with Property Grid would be nice.
Thank you,
Branko
33 Answers, 1 is accepted
We have introduced additional DataAnnotations support for RadPropertyGrid that will be included in our next official release Q1 2012. It will work as expected - for example defining a property with Browsable false will make the property not be generated in the grid:
private string name;
[Browsable(false)]
public string Name
{
get { return this.name; }
set
{
if (value != this.name)
{
this.name = value;
this.OnPropertyChanged("Name");
}
}
}
Kind regards,
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

[Browsable] is one of the Attribute, what are the other attributes that we can work with ?
I am validating RadPropertyGrid control and I am trying to set it up in a proper way. I need to work with attributes. Example, is when AutoGeneratePropertyDefinitions="True", Auto Generated Properties in the Property Grid are generated based on, input type of variables.
How to handle error state and validation using attributes, if the input property is not correct. Is not in the range, or has expression which is not correct, etc ?
How to write Description for the property, but not from xaml !?
<
telerik:RadPropertyGrid
AutoGeneratePropertyDefinitions
=
"True"
Item
=
"{Binding Path=DataElement}"
>
</
telerik:RadPropertyGrid
>
// I would like to define range for the type of DateTime as Attribute . If the date is not in
// the proper range, show predefined or customized ErrorMessage,
// as a tooltip, marking the field in red. Similar to RadMaskedDateTimeInput control.
private DateTime dateTimeValue;
[Range(typeof(DateTime), "01.01.1900", "01.01.2099")]
public DateTime DateTimeValue
{
get { return dateTimeValue; }
set
{
dateTimeValue = value;
OnPropertyChanged("DateTimeValue");
}
}
Thank you,
Kind Regards,
Branko
We have added support for IsReadOnly, Browsable, Description, ValidatesOnException, NotifyOnValidationErrors, Display attributes.
You can define the description like follows:
[Browsable(true)]
[Description("The department the employee is associated with.")]
[Display(Name = "Department", Order = 2, GroupName = "Job Description")]
[ReadOnly(true)]
public int DepartmentId
{
get
{
return departmentId;
}
set
{
if (this.departmentId != value)
{
this.departmentId = value;
}
}
}
All the best,
Maya
the Telerik team

Thank you for response. I have one more question. In new release, will we be able to write our own attributes ?
Thank you.
Best regards,
Branko
Could you provide a bit more details about the exact scenario that you want to accomplish ?
Maya
the Telerik team

We would like to accomplish something like this.
public bool IsLastNameRequired { ... }
[RequiredIf("IsLastNameRequired", true, ErrorMessage = "Last name is required.")]
public string LastName
{
get { return this.lastName; }
set
{
this.lastName = value;
this.OnPropertyChanged("LastName");
}
}
[RequiredIf (..)] is custom attribute. And we can use it for validating.
Do you support IDataErrorInfo inteface in RadPropertyGrid ?
Thank you.
Best regards,
Branko
If you target implementing custom validation based on attributes, our recommended approach is to use the CustomValidationAttribute. I have prepared an example project for you that demonstrates how to achieve this. However, you should use 2012.1.215, or a newer version, in order to benefit from this feature. As a side note, please let me bring to your attention that we have planned to introduce a few validation improvements, including a validation summary for RadPropertyGrid.
All the best,
Ivan Ivanov
the Telerik team

Do you support IDataErrorInfo interface in Property Grid? This interface is very appropriate to use for validation in MVVM design.
Please take a look at this example, similar solution we want to achieve implemented in Property Grid. This is nice example of self written attribute (
[RequiredIf(
"IsLastNameRequired"
,
true
, ErrorMessage =
"Last name is required."
)]
), and validation of that attribute.http://blog.cylewitruk.com/2010/10/cross-field-attribute-validation-in-wpf-using-mvvm-and-idataerrorinfo/
Looking forward your kind answer. Thank you,
Best regards
Branko
Currently RadPropertyGrid does not support DataErrors validation (It supports exception validation only). However, as it is not a major feature we can implement it for you and include it in an internal build. I believe that we will manage to introduce this change in two weeks. I will keep in touch with you, in order to notify you as soon as the mentioned IB is available for download.
Regards,
Ivan Ivanov
the Telerik team

Thank you for the answer. I am waiting your internal build. In the meanwhile I would like to try Attributes that you have integrated in this last release. Where I can find more about attributes, type of attributes that you are currently supporting in Property Grid?
P.S. The goal of my project is, by selecting the object to present properties of selected object: example ObjA (5 properties), ObjB (10 properties), ObjC(7 properties). Type of properties can be, bool, decimal, float, string etc. And then apply validation on those properties from selected object. As I have mention we are MVVM oriented, and validating those properties would be nice with IDataErrorInfo interface.
If you have any other suggestion on this scenario please inform me.
Thank you,
Best regards,
Branko
The following demo: demos > RadPropertyGrid > "DataAnnotations support" illustrates RadPropertyGrid's native compatibility with several attributes: ReadOnly, Display, Description, Category, Browsable. The control also supports exception based property validation, so that you can implement validaition logic using the DataAnnotatins validation attributes (Required, Range, Regex, CustomValidation etc.). You can refer to the attached project from the previous post that illustrates how to implement validation based on the CustomValidation attribute. Basicly attribute-based validation should be fully MVVM comliant, so that I believe that it might be an appropriate approach in your scenario.
Kind regards,
Ivan Ivanov
the Telerik team

We are waiting your Internal Build for DataErrors validation. Just to clarify one more time, we would like if you could implement PropertyGrid interaction with IDataErrorInfo interface, and we will then make our error handling using that interface and Property Grid.
Regards,
Branko
I will notify you as soon as the IB is released (it should be the next Monday/Tuesday). I will prepare a simple test project for your referense, as well. Please, do not hesitate to contact us if you have any further inquiries.
Kind regards,
Ivan Ivanov
the Telerik team
I have prepared an example project for you with a type that implements IDataErrorInfo, using the latest IB binaries. As I have mentioned in the other support thread of yours, we are up to add a validation summary to RadPropertyGrid, so that users could have a summarized view of errors' messages.
Kind regards,
Ivan Ivanov
the Telerik team
.jpg)
You can handle RadPropertyGrid's AutoGeneratingPropertyDefinition event and execute the following code:
I have modified the sample project with our latest official binaries and now it is working as expected. You can find it attached.
Kind regards,
Yoan
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
But I can not get it to work with "normal" validation attributes.
[Required(ErrorMessage = "Name is required")]
[RegularExpression(@"^(?:\w*\d*[_]*)$", ErrorMessage = "Input format is: a-z, A-Z, 0-9, _")]
public string Name { get; set; }
You can manually apply the validation by using the Validator class. You can call the ValidateProperty method on the set accessor of a property to check the value against the validation attributes for the property. Please note that you must also set both ValidatesOnExceptions and NotifyOnValidationError properties to true when data binding to receive validation exceptions from validation attributes. The following code snippet demonstrates how to use data annotation attributes to validate a property value:
[Range(6,26)]
public int Age
{
get { return this.age; }
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age"});
age = value;
}
}
Regards,
Yoan
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
We can then use the same object in MVC4 and WPF without writing ugly code in the setters
For propertychanged we are using Fody. so every property is just plain get;set;
Please implement IDataErrorInfo as you promised a year ago, you can read it her
Happy coding
Could you please be more specific? You are mentioning both IDataErrorInfo and the DataAnnotations validation attributes? Are using the latter in order to validate and retrieve the errors in the IDataErrorInfo error indexer? As far as I can see a colleague of mine has sent you a sample project with IDataErrorInfo and it is working fine. As for this FluentValidation framework, we cannot claimsuppor for it.
Kind regards,Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
Hi Branko,
I will notify you as soon as the IB is released (it should be the next Monday/Tuesday). I will prepare a simple test project for your referense, as well. Please, do not hesitate to contact us if you have any further inquiries.
Kind regards,
Ivan Ivanov
the Telerik team
If you implement the interface IDataErrorInfo you can sit back and let the developer implement the error handling any way he choose.
I don't think that I should be forced to write code like this:
[Range(6,26)]
public
int
Age
{
get
{
return
this
.age; }
set
{
Validator.ValidateProperty(value,
new
ValidationContext(
this
,
null
,
null
) { MemberName =
"Age"
});
age = value;
}
}
I want this
public Int Age {get; set;}
.jpg)
I am afraid that for the time being we cannot suggest you a phone support. RadPropertyGrid supports IDataErrorInfo as the entire control's functionality relies on data binding and the framework Binding natively supports DataError notifications. In case you want to use both DataAnnotation attributes and IDataErrorInfo and you want to use DataError validation rather than exception-based validation, you can use the Validator.TryValidateObject method in order to retrieve the errors and pass them through IDataErrorInfo errors indexer. It should be something like this:
public string this[string columnName]
{
get
{
List<
ValidationResult
> results = new List<
ValidationResult
>();
Validator.TryValidateObject(this, new ValidationContext(this, null, null), results);
if (results.FirstOrDefault(r => r.MemberNames.Contains("Name")) != null)
{
return ". . .";
}
else
{
return string.Empty;
}
}
}
Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
From your last post I could read that the propertyGrid support the IDataErrorInfo interface so I created a small test project to test it out.
This is what I found:
- If I bind to a single object then validation is working, in PropertySetMode="Intersection" and PropertySetMode="None" mode.
- If I bind to an ObservableCollection with two object, then validation is not working any more.
Whit two or more object I know that you are creating a dynamic object and I think that this dynamic object is not implementing IDataErrorInfo.
Another problem with the dynamic object are that it's propagate changes out but it's not listen to changes made to the object.
Are we getting closer to a real problem definition?
Just for fun I have attached a picture of the form designer I am building. I am close to be finish but I am missing validation and true 2-way binding in the PG.
Well, it makes sense. I have not figured that you are using the PropertySets feature. The current implementation of this feature utilizes an internal implementation of DynamicObject to represent the union/intersection set of dynamic properties. Another weakness of this approach is the lack of attribute support. Unfortunately, introducing a general internal implementation of IDataErrorInfo, will be quite a difficult task as validation relies on rules that are scenario-specific. We had some ideas about reworking this feature by using ICustomTypeProvider and adding more customization options to the public API. I will be able to tell you more on this matter as soon as we are with the Q2 development. We are going to do some research on this feature and will contact you back until the end of May.
Regards,Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
First we need a setting on the PG: ValidateOnAllPropertySetModes = "True"
Then maybe a call-back before a value is set: string (object valueToSet, IList<object> listOfSelectedObjects) that returns string.empty if there is no errors, and a string with the error description if there is an error.
If the call-back is not set then resolve the errors like this.
We have selected two object that both has public int Height {set;get;} property.
Object one has this rules
- Required == true
- Interval between 10 and 200
Object two has this rule
- maxheight = 200
Test's
Set Height = 5
obj2 is happy, obj1 is not, so the input value 5 is rejected and the error message for obj2 is displayed.
set Height = 10
Everybody is happy, set the value.
I would like to help you out with coding or as a tester if it can speed up the process.
.jpg)
- Validate more than one object at a time. Using IDataErrorInfo
- easy way to disable a property editor based on a value in another property editor
- You don't have to leave the property editor before the value is set on the underlying object.
- If the underlying object is manipulated the changes are reflected in the PropertyGrid.
Are U up for the challenge?
Thank you for expressing your requirements and ideas, Martin. We are almost done with the development of Q2, so we will be able to research this feature in the close future. We have come with an idea to expose an API that would import of a custom validation ruleset, which would be used by our DynamicObject's IDataErrorInfo implementation. I will send you a demo as soon as we have made any significant progress.
Kind regards,Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
.jpg)
We will take this into account. Thanks.
All the best,Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

I tried your attached project which demonstrates a custom validator for a RadPropertyGrid. The example works as it shows a red border around the Textbox if you enter an invalid value. But the error message ("The input value should be odd integer and should be in specified range: {10, 20}.") is not shown!
Worse: I built the with our current commercial Telerik WPF-Controls version 2013.3.1204.40 and now not even the red border is shown!
How do I use RadPropertyGrid with property level validation???
It works with IDataErrorInfo, but that is not what I want I'd like to give validation hints to the user for validation logich in the property setters (
Validator.ValidateProperty)
Thank you for the feedback.
I've modified the project with the following code and the validation border and message are shown as expected:
private
void
radPropertyGrid_AutoGeneratingPropertyDefinition(
object
sender, Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e)
{
(e.PropertyDefinition.Binding
as
Binding).ValidatesOnExceptions =
true
;
(e.PropertyDefinition.Binding
as
Binding).NotifyOnValidationError =
true
;
}
Let me know if you have additional questions.
Regards,
Yordanka
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>

thank you. these two lines were missing. Now the example is working like expected.
Regards from Düsseldorf, Germany.