This is a migrated thread and some comments may be shown as answers.

Validate fields from PropertyGrid using ValidationRule

3 Answers 241 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Adriana
Top achievements
Rank 1
Adriana asked on 31 Jul 2014, 04:00 PM
Hi guys,

The problem is that in the property box we have, when the user types a string in a field that’s bound to a float property the by default  error is displayed. The customer wants  to display some nice user friendly errors.
 
I have a property grid (called AssociatedObject), and for this property grid I add the PropertyDefinitions from code (using a Behavior)  like this:

The AssociatedObject is of type RadPropertyGrid

foreach (CustomerCustomFieldVMv customField in propertyItemsForCompanyList)

            {

                Binding bind = new Binding();

                switch (customField.ValueType)

                {

                    case CustomFieldValueType.Float:

                       
                 //custom field is of type float

                       
              bind = new Binding

                          
           {

                               Source =customField,

                               Path = new PropertyPath("FloatValue"),

                               Mode = BindingMode.TwoWay,

                              
                              UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,

                              
                              ValidatesOnDataErrors = true,

                              
                              NotifyOnValidationError = true

                          
};

                        bind.ValidationRules.Add(new NumericFieldValidation());

                       
                    break;

                    case CustomFieldValueType.String:

                       
                                     //custom field is of type string

                       
                                    bind = new Binding

                           {

                               Source =customField,

                               Path = new PropertyPath("StringValue"),

                               Mode = BindingMode.TwoWay

                          
                    };

                       
                     break;

                    default:

                       
                    break;

                }

                var newDefinition = new PropertyDefinition()

                         
{

                              Binding = bind,

                              DisplayName =
customField.Description,

                              IsReadOnly = false

                         
};

 

               
AssociatedObject.PropertyDefinitions.Add(newDefinition);

            }

The FloatValue property is of type float and the StringValue is of type string. The values are bind and displayed correctly.

 

The class where I want to do the validation is NumericFieldValidation. It’s looking like this:

public class NumericFieldValidation : ValidationRule

    {

        private string invalidInput = “error wrong format”;

 

        // Implementing the abstract method in the Validation Rule class

        public override ValidationResultValidate(object value,System.Globalization.CultureInfo cultureInfo)

        {

            float val;

            if(!string.IsNullOrEmpty((string)value))

            {

                // Validates weather Non numeric values are entered as float value

                if(!float.TryParse(value.ToString(),out val))

                {

                    return new ValidationResult(false,invalidInput);

                }

            }

 

            return new ValidationResult(true,null);

        }

    }

In order to display the errors visual studio throws I use tooltip messages.

<telerik:RadPropertyGrid.Resources>

                <Style TargetType="{x:Type TextBox}">

                    <Style.Triggers>

                       
<Trigger Property="Validation.HasError" Value="True">

                           
<Trigger.Setters>

                                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors).CurrentItem}"/>

                                <Setter Property="BorderBrush" Value="Red"/>

                                <Setter Property="Background" Value="#FFCCCC"/>

                           
</Trigger.Setters>

                        </Trigger>

</Style>

</telerik:RadPropertyGrid.Resources>

 

The problem I have is that when I type something in the FloatValue field inside the property grid I would like it  to go to the Validate method from NumericFieldValidation, but it’s not. It ignores the Validation rule I added for the FloatValue property.

Can you please help me with a solution? It would be very helpful if the binding would send the user to NumericFieldValidation class for validation.

Best regards,
Adriana
 

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 04 Aug 2014, 02:03 PM
Hello,

I can suggest you using DataAnnotations validation and framework's validation interfaces which are more mature approaches than the Validation Rules mechanism.
I attached an example project demonstrating validation logic based on the CustomValidationAttribute.
How does this work for you? 


Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Adriana
Top achievements
Rank 1
answered on 04 Aug 2014, 02:19 PM
Thank you for the example. Very helpful. In my case the property is of type double and i want to validate it against some string User inserts in the property box.
In the example you gave me let's say we want to bind the property box to StadiumCapacity property which is of type int and the user inserts a string. If the user enters a string we want to display "Please insert a number!". In this case the set of StadiumCapacity property won't be called and the custom validaton won't show. 
Can you please help me with this case?
0
Dimitrina
Telerik team
answered on 05 Aug 2014, 02:41 PM
Hi,

In that case the framework validation should be raised as a string value cannot be assigned to a numeric property. The Binding of a PropertyDefinition will take care of this framework validation and such an error message will be shown out of the box.
In this case, the setter of the property will not be invoked at all. You can also test entering a string value for a numeric field on our WPF Demos with RadPropertyGrid.

If this is not your case, would you please extend my demo project or create a new one to better illustrate your question and send it to me?
You can open a new support thread and attach the sample there.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
PropertyGrid
Asked by
Adriana
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Adriana
Top achievements
Rank 1
Share this question
or