WPF 4: Custom Command and Command Parameter for TextBox using Prism 4

Posted by: Mahesh Sabnis , on 7/11/2011, in Category WPF
Views: 108638
Abstract: Using Prism 4 application libraries, it is possible for us to attach custom Command and Behavior to any control. This supports code-less application development. In this article, we will use this concept to attach command and behaviour to a WPF Text Box.

Lately I have been more inclined towards Client application development using WPF and Silverlight and I am enjoying my experimentations with various controls and its behaviors. A couple of days ago, when I was conducting a WPF and Prism 4 training, a participant asked a question on attaching a custom command property for an input control like textbox. The scenario discussed was  a WPF UI had a TextBox to accept input for data filtering (search the matching record.). The application was to be developed using WPF and Prism 4. The Prism 4 framework provide libraries using which loosely-coupled application with MVVM pattern can be developed.

Note: I have written an introductory article in the past for Silverlight too Prism 4: Defining Custom Command Behavior for ListBox control in Silverlight 4.0 and for WPF WPF 4: Developing End-to-End application using Prism 4 Commanding and DataGrid Custom Behavior

For this article I have used the following Prism 4 libraries:

  • Microsoft.Practices.Prism.dll.
  • Microsoft.Practices.Prism.Interactivity.dll.
  • Microsoft.Practices.Prism.UnityExtensions.dll.
  • Microsoft.Practices.ServiceLocation.dll.
  • Microsoft.Practices.Unity.dll

Let us get started.

Step 1: Open VS2010 and create a WPF windows application, name it as ‘WPF4_Custom_Command_Behavior_TextBox’. Add the libraries listed above in the project. In the MainWindow.xaml, add a TextBlock, TextBox and DataGrid as below:

WPF layout

Step 2: In the project, add a new class file and call it ‘DataClasses.cs’. Add the following classes shown below. These classes are used as data stores.

wpf-custom-command

Step 3: In the project, add a new class file and call it ‘CustomCommandBehaviorRepository.cs’. Now add a Command Behavior class for the TextBox event. Since the custom Command property is to be executed for the TextChanged event, the custom command behavior class must raise the TextChanged event as shown below:

textbox-command-behavior

The CommandBehaviorBase<T> is used to define a base behavior for connecting the control type to Command.

Step 4: We now need to specify the class for defining Command and CommandParameter property. This class also defines methods to set behavior for the TextBox. Command, CommandParameter and CommandBehavior properties are added as Dependency properties. The code is as shown below:

text-changed-event-prism

text-changed-event-prism1

Step 5: Now we need to add a class for providing data to the UI element. This class should define the DelegateCommand<T> object which is bound with the textbox in XAML. This command object is responsible for executing action from the UI and making data available to the UI. The code is as below:

wpf-inotifyproperty-changed

wpf-inotifyproperty-changed1

Note: Please read the comments carefully. It explains the role of every method.

Step 6: Open MainPage.Xaml and create an instance of the ApplicatonManagement class in Xaml. Also define Command and CommandParameter property of the TextBox as shown below:

wpf-xaml-custom-textbox

Step 7: Run the application and the following result will be displayed:

WPF Commanding Example

 

Enter product name in the TextBox and the result is as shown below:

WPF Commanding Example

Conclusion: Using Prism 4 application libraries, it is possible for us to attach custom Command and Behavior to any control. This supports code-less application development.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!