SIGN UP MEMBER LOGIN:    
ARTICLE

Adding Custom Control Dynamically in LightSwitch

Posted by Jaganathan Bantheswaran Articles | VS LightSwitch 2011 July 19, 2011
In this article we shall see about how to add the custom control dynamically to the LightSwitch screen.
Reader Level:

Prologue:


My previous article discussed how to bind the custom control to the screen property in a LightSwitch screen. In this article we shall see how to add the custom control dynamically to the LightSwitch screen.

We can take the LightSwitch demo application we created in part I of this article series. As we have discussed in part I, create a demo application with a custom control.

We have added the custom control, if you want to do something with the control; we need to access the custom control. So access the custom control as we discussed in this article.

So we have accessed the custom control, now we need to bind the control with the screen property [entity property], do this stuff as we did in the last article.

In this let us discuss how to add custom controls dynamically.

If you see the output of the article part I, the gender property is bound with the check box. So instead of displaying a check box we are trying to display the radio buttons to select male or female.

Updating the Entity in LightSwitch:


As shown in the above figure, change the Gender property type "Boolean" to "String".

Delete the Gender property from Screen:

As we are going to add the radio buttons instead of check box, delete the gender check box as shown in the figure.
CusContDynaLS2.gif

Adding StackPanel to Screen:


We need to add a stack panel to the LightSwitch screen as a custom control to hold the radio buttons.
CusContDynaLS3.gif

As shown in the above figure, just add the StackPanel control as we did for Button control.

Before accessing the StackPanel, we need to add the namespaces to the code-behind. So refer to the article part III to add the namespace.
 

Adding Radio Buttons Dynamically:


CusContDynaLS4.gif

In the given following figure, we have added the radio buttons dynamically.

Here,
  • We are getting the StackPanel control and setting the Orientation to Horizontal.
  • We are creating two Radio buttons for male and female.
  • Setting the content of the radio buttons as male and female respectively.
  • If the user clicks on the male radio button, "Checked" event will be fired to set the Gender property to "Male".
  • If the user clicks on the female radio button, "Checked" event will be fired to set the Gender property to "Female".
  • Add the radio buttons to the StackPanel.

Complete Screen tree:

The following figure shows the complete screen tree for this article parts.

CusContDynaLS5.gif

Hit F5 to see the application in action.

Application in Action:


CusContDynaLS6.gif

You can see the male and female radio buttons added into the LightSwitch screen.

Summary:


In this article part IV, we have seen how to add a custom control dynamically to the LightSwitch screen.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which make me to give a better article next time.

Thanks.

Login to add your contents and source code to this article
share this article :
post comment
 

This article itself a good example for Radio Button usage in LightSwitch. We can use Silverlight or Windows controls in LightSwitch. We need to add the controls you need using Custom Control option in LightSwitch

Posted by Jaganathan Bantheswaran Jan 19, 2012

Hi Jaganathan Thank you for your article. You give me good advice on how I can implement radio buttons with LightSwitch. Urs

Posted by Urs Gloor Jan 19, 2012

Please read my previous articles. LightSwitch Application is different from other applications. You cant see the source code files directly in the Solution. Just open the CreateNewPerson screen and in top right corner you can see the Write Code dropdown, from this select CreateNewPerson_Created link. Now you will be having the code written by me.

Posted by Jaganathan Bantheswaran Dec 22, 2011

Obviously it may give errors. I didn't handled any erroneous scenarios in all of my sample applications. i just showed you that "How To Do".

Posted by Jaganathan Bantheswaran Dec 22, 2011

Obviously it may give errors. I didnt handled the erroneous scenarios in all of the sample applications. I just explained "How To Do".

Posted by Jaganathan Bantheswaran Dec 22, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor