Bind screen control extension TextBox.MaxLength to 'Maximum Length' property on an entity field
-
Donnerstag, 12. Juli 2012 15:28
Hi,
I have a screen control extension with a Textbox and was hoping bind the TextBox MaxLength attribute to the string field's "Maximum Length" value as set in the designer.
For example, let's say we have an entity "Contact" with a string field "FirstName" where the "FirstName" field has a Maximum Length of 100.
What is the best way go about binding the screen control extension TextBox.MaxLength attribute to Contact.FirstName field's Maximum Length?
Thanks,
Pat
- Bearbeitet PatSturmer Donnerstag, 12. Juli 2012 15:38
- Bearbeitet PatSturmer Donnerstag, 12. Juli 2012 18:56
Alle Antworten
-
Freitag, 13. Juli 2012 03:35Moderator
There isn't a straight forward way of binding to the max length information. You will have to bind to the detail information of the property to which your control is bound, find the applicable max length attributes, and return the max length if one was found. Here's a sample that shows how to make a converter to get the max length information from the details of the property:
public partial class CustomersListDetail { partial void CustomersListDetail_Created() { IContentItemProxy proxy = this.FindControl("Name"); proxy.SetBinding(TextBox.TextProperty, "Value", BindingMode.TwoWay); proxy.SetBinding(TextBox.MaxLengthProperty, "Details", new DetailsToMaxLengthConverter(), BindingMode.OneWay); } } public class DetailsToMaxLengthConverter : IValueConverter { #region IValueConverter Members object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { IEntityProperty propertyDetails = value as IEntityProperty; if (null == propertyDetails) return 0; IEntityPropertyDefinition propertyDefinition = propertyDetails.GetModel(); int? minValue = propertyDefinition.Attributes .OfType<IMaxLengthAttribute>() .Min(maxLength => maxLength.Value); return minValue.GetValueOrDefault(0); } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } #endregion }Justin Anderson, LightSwitch Development Team
- Als Antwort markiert PatSturmer Freitag, 13. Juli 2012 12:58
-
Freitag, 13. Juli 2012 13:09
The converter worked perfectly... many thanks!

