Microsoft Lightswitch select entity property name in combobox

Posted in LightSwitch by tejana on May 13, 2011

We can use the comboBox control from the System.Windows.Control namespace to create a data entry screen using a ComboBox of property names for an  entity to fill a string field. Following on from yesterday’s post, I have added an entity to our project named:  “EntityPropertyNames” with ID, Name, and EntityName, with a query named “EntityPropertyNamesByEntity” with filter of EntityName = new parameter.  Adding the query to our CreateNewDataEntry screen and also adding “EntityProperty” to the NewDataEntry entity:


I have set the Query Parameter of the EntityPropertyNamesByEntity to the Entity Name of the DataEntryProperty and changed the control type of the Entity Property to a combobox. We want to set the property names based upon the entity selected so we set the DataEntryProperty.EntityProperty to read only unless the DataEntryProperty.EntityName has been set:

public partial class DataEntry
      partial void EntityProperty_IsReadOnly(ref bool result)
          result = EntityName == null;

Once the EntityName is set we are ready to find the property names:

partial void DataEntryProperty_Validate(ScreenValidationResultsBuilder results)
           //do not process on load (null) or on save (EntityPropertyNamesByEntity already set correctly)
           if ((DataEntryProperty.EntityName != null) &&
                  StringComparison.Ordinal))) == 0)
               //This query is not autoload because changes to parameter made by
               //custom control combo box are not picked up 

               Microsoft.LightSwitch.Model.IEntityType entityType =
                   .Single(e => e.Name.Equals(DataEntryProperty.EntityName, StringComparison.Ordinal));
               foreach (string s in
                   entityType.Properties.Select(p => p.Name)
                   .Except(EntityPropertyNamesByEntity.Select(ep => ep.Name)))
                   EntityPropertyNames epn = new EntityPropertyNames();
                   epn.Name = s;
                   epn.EntityName = entityType.Name;

               IContentItemProxy comboBox = this.FindControl("EntityProperty");

                   "Screen.EntityPropertyNamesByEntity", System.Windows.Data.BindingMode.TwoWay);
                  "Screen.DataEntryProperty.EntityProperty", System.Windows.Data.BindingMode.TwoWay);



Special thanks to Microsoft’s Justin Anderson for the Model queries: http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/b35d9341-0170-4e39-9a1b-47d57e94c527 for VB code see here.

and Tim Leung’s excellently clear post about binding to a ComboBox: http://dotnettim.wordpress.com/2011/05/09/lightswitch-limiting-item-selections-using-a-combobox/


One Response

Subscribe to comments with RSS.

  1. dotnettim said, on May 13, 2011 at 1:46 pm

    Thanks Mike – that’s a really interesting write up!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: