Silverlight 4.0: Applying TextChange behavior to the TextBox
Posted by: Mahesh Sabnis ,
on 7/17/2011,
in
Category Silverlight 2, 3, 4 and 5
Abstract: This article demonstrated how to define a custom behavior and extend the functionality of the Silverlight controls like the TextBox
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:
The above class defines the behavior for the TextBox element. The generic class Behavior<T> is currently typed to the textbox. The AssociatedObject represents the object to which the behavior is to be attached. Currently it attaches the TextChange event for the TextBox.
Step 3: In the same class file added above, add following two classes which act as a Data Source:
The EmployeeCollection class defines Employees as an ObservableCollection<T> object which is initialized to some Employee records using the constructor. This is used to bind Employee data to the DataGrid. The public property EmpName will be used to filter Employees from the Employees property. It defines some complex logic to run an Employee filter. I have added comments in the code so please read it carefully. I have declared a temporary ObservableCollection<Employee> property of the name EmployeedFiltered, which will be used to store Employees data during the TextChanged event filtration. In a real scenario, if you use WCF Service to fetch data, then the logic will be much easier.
Step 4: Open MainPage.Xaml and define System.Windows.Interactivity and the current application’s namespace as below:
Step 5: Drag-Drop the DataGrid and define columns in it binding to the public properties of the Employee class created above. The xaml code will be as shown below:
If you observe the XAML above, the TextBox txtEmpName is set to the behavior as shown here:
The above lines implies that when the TextBox object is drawn on the UI, the behavior with TextChange event will be associated with it. So now when the user enters any text in the TextBox, the TextChange event will be fired and the Binding property ‘EmpName’ will be updated.
Step 6: Run the application and the result will be as below:
Now enter some text in the TextBox and the Result will be as shown below:
Conclusion: By defining a custom behavior, we can extend the functionality behavior of the Silverlight Element system.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @
maheshdotnet or connect with him on
LinkedIn