In this article, we shall discuss about how to handle the Property Changed Event
in LightSwitch 2011.
To demonstrate the Property Changed Event, let us create a LightSwitch 2011
application which has a screen to save a student's details. In this application we
will have a choice list for student Entry Mode.
The Entry Mode will have two options, one is Regular Entry and the other one is
Lateral Entry. When the user selects the Entry Mode, depending on the value
selected, we need to display some values or controls.
Preparing the Solution:
Startup VS LightSwitch 2011, create a project with the name
"HowToHandlePropertyChangedEventInLS2011" as shown in the below figure.
Follow the steps as shown in the figure.
As shown in the figure, create an entity "Student" with the shown properties and
make sure the two properties DiplomaPercentage and HSSPercentage have the required option
unchecked, because these mentioned properties may or may not be filled.
Adding Choice List Options:
Since the EntryMode property is a choice list, we need to add to the choice
To add the choice list,
- Open the Entity Designer.
- Select the EntityMode property.
- Open the Property window for the selected property.
- As shown in the figure select the Choice List from the property window.
In the Choice List windows, add the two options shown in the figure above.
Create a New Data Screen as shown in the figure.
- Select the "New Data Screen" template from Screen template panel.
- Name the Screen "CreateNewStudent".
- Select the Entity for the Screen as Student and Click OK to create the screen.
As we are going to display some options
depending on the selection of our Choice List, we need make some changes in the
When the screen is displayed, the Diploma percentage text box will be invisible
by default. So we need to make that invisible.
- Open the Screen Designer. Select the Diploma Percentage control and go to the Property window.
- In Property window, uncheck the Is Visible property to make the control invisible by default.
Make a group with two properties
"DiplomaPercentage" and "HSSPercentage".
Here, to add the Group,
- Select the Root Screen Element called "Student Property"
- Press the "Add" dropdown like button.
- Select the "New Group" option from the dropdown list.
The Complete Screen Tree:
The above figure shows the complete screen design.
To handle the property changed event, we need to go for the code behind.
To add the code for handle the property changed event, we need to open the
"CreateNewStudent" class as shown the figure given below.
- In the Screen Designer, Click on the "Write Code" dropdown list which is on the top.
- Select the CreateNewStudent_Created option. This method will be executed just after the screen is displayed.
Handling the Event:
The below screen shot shows the way in which we have handled the property
- In the CreateNewStudent class, we have the CreateNewStudent_Created method. In that method, we cast the entity which is bounded with screen as an INotifyPropertyChanged interface which will give the property changed event. In our application, we have casted the StudentProeprty as INotifyPropertyChanged and defined an event handler.
- In the PropertyChanged event handler, the PropertyChangedEventArgs's object has the PropertyName property. Using that property, we can handle the functionality we need.
In our demo application, based on the selection
of the choice list, we are going to display the HSSPercentage or
Application in Action:
So we are done. Hit F5 to see the Application in action.
The above figure shows the Diploma Percentage control on selection of the
Lateral Entry option.
The below figure shows the HSS Percentage control on selection of the Regular
In this article, we have seen about how to handle the PropertyChanged event in a Visual Studio LightSwitch 2011 application.
Thanks for spending your precious time here. Please provide your valuable
feedbacks and comments, which make me to give a better article next time.