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