Tejana

Microsoft Lightswitch Entity Name ComboBox

Posted in .net, LightSwitch, Microsoft, Software, Visual Studio 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 project entity names to fill a string field. I have created a LightSwitch project with three enitities and one screen CreateNewDataEntries screen:

lsEntityNameComboBox

On the CreateNewDataEntries screen I have added the EntityNames query as a data item and changed the control on the field for the DataEntryProperty.EntityName to a System.Windows.Control.ComboBox.

CreateNewDataEntry

The following code is added to the CreateNewDataEntry.cs:

partial void CreateNewDataEntry_Activated()
       {
           foreach (string s in
               this.Application.Details.GetModel().GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Select(e => e.Name))
           {
               {
                   EntityName n = new EntityName();
                   n.Name = s;
               }
           }

           IContentItemProxy comboBox = this.FindControl("EntityName");
           comboBox.SetBinding(
                   System.Windows.Controls.ComboBox.ItemsSourceProperty,
                   "Screen.EntityNames", System.Windows.Data.BindingMode.TwoWay);
           comboBox.SetBinding(
                   System.Windows.Controls.ComboBox.SelectedItemProperty,
                   "Screen.DataEntryProperty.EntityName", System.Windows.Data.BindingMode.TwoWay);
       }

partial void CreateNewDataEntry_Saving(ref bool handled)
        {
            if (DataEntryProperty.EntityName != null)
            {
                foreach (EntityName e in EntityNames)
                {
                    e.Delete();
                }
            }
        }

 

This approach yields the desired results :

 

run

 

I wish I had a group to discuss this with. The entity EntityName is never really used to save data. The model is queried for the entity names which are then put into the screen collection of EntityNames but these items are deleted before any save on the screen. If you wanted to save the entity names you could remove the partial void CreateNewDataEntry_Saving(ref bool handled) and add the .Except:

partial void CreateNewDataEntry_Activated()
       {
           foreach (string s in
               this.Application.Details.GetModel().GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Select(e => e.Name)
               .Except(EntityNames.Select(e=>e.Name)))
           {
               {
                   EntityName n = new EntityName();
                   n.Name = s;
               }
           }

           IContentItemProxy comboBox = this.FindControl("EntityName");
           comboBox.SetBinding(
                   System.Windows.Controls.ComboBox.ItemsSourceProperty,
                   "Screen.EntityNames", System.Windows.Data.BindingMode.TwoWay);
           comboBox.SetBinding(
                   System.Windows.Controls.ComboBox.SelectedItemProperty,
                   "Screen.DataEntryProperty.EntityName", System.Windows.Data.BindingMode.TwoWay);
       }

The binding of the ComboBoxItemSource is to this Screen property “Screen.EntityNames”. This is the only real use of the definition. I wish I knew how to create a binding with a code only variable, but at least this binding works.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: