Binding Textbox.MaxLength to Maxlength Validation rule

Binding Textbox.MaxLength to Maxlength Validation rule

Old forum URL: forums.lhotka.net/forums/t/8647.aspx


Devman posted on Thursday, March 11, 2010

Hi,

We have Maximum Length validation rules for some properties in our business object. However what we would like to do ideally, is bind MaxLength on a textbox to this validation rule to stop invalid lengths being entered in the first place. Has anyone else tired this?

Any help would be greatly appreciated.

Regards

David

RockfordLhotka replied on Thursday, March 11, 2010

You can get at the rules attached to a property through the ValidationRules object (inside your business object). You can get a list (string[] array) of rule:// URI items that describe all the rules attached to all properties for the object.

You can then use the RuleDescriptor type to parse the rule:// URI values into parts, including things like the property name, rule name and rule arguments.

That should make it relatively easy to find the StringMaxLength rule items and get the max length value for each.

Once you have that information, you then need to design an API to expose the information to the UI in a way that your UI can consume the information.

If you are in SL or WPF you could possibly subclass PropertyStatus and just add a MaxLength property, then bind the TextBox control's length property to PropertyStatus.

If you are in another UI technology you'll need a slightly different approach, but the concept would be similar.

maridob replied on Tuesday, June 07, 2011

 

I wrote this in my blog, hopefully it will help somebody :-)

Copyright (c) Marimer LLC