This is a mirror of official site: http://jasper-net.blogspot.com/

Silverlight 4.0: Applying TextChange behavior to the TextBox

| Tuesday, July 19, 2011
I was asked a question recently about updating the binding source property during the TextChanged event, for the textbox. All those who are using Silverlight may be aware that when the source property is bound with the Textbox’s Text property, then to update the source property value, we must use the LostFocus event on the TextBox. However if you need to make it possible during the TextChanged event, then you need to define custom interactive behavior for the TextBox.

To attach a custom behavior to the TextBox, we need to add a reference to the System.Windows.Interactivity.dll file in our Silverlight project. This is available in Microsoft SDK’s Expression folder as below path:

c:\Program Files (x86)\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries\System.Windows.Interactivity.dll

This dll provides various classes for element behavior, which can be customized for control specific event. One of the classes we are using in this article is the Behavior class. This class encapsulates state information and ICommands into the attachable object and performs some action when the control raises a specific event. E.g. If we attach a behavior with the TextBox and its TextChanged event, then during the execution when the end-user enters any text in the TextBox, the behavior associated with the TextChanged event will be automatically executed. Let us see some code:

Step 1: Open VS 2010 and create a new Silverlight project. Name the project as ‘SL4_TextBox_Interactivity_Behavior’. In this project, add a reference to ‘System.Windows.Interactivity’ dll.

Step 2: In the project, add a new class file and name it as ‘Data_Behavior_Classes.cs’. This file will contain the TextBox behavior class and data source classes as below:

silverlight-textchanged-behavior.png


Read more: dot net curry
QR: ShowArticle.aspx?ID=737

Posted via email from Jasper-net

0 comments: