advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the WPF Command Framework example source code.
Partners & Affiliates
advertisement
advertisement
advertisement
Rate this item | 0 users have rated this item.
Email this articleEmail this article
 
Introduction to the WPF Command Framework
The command framework in WPF provides a set of powerful tools that let developers build more intelligent user interfaces. 

advertisement
indows Presentation Foundation (WPF) not only lets you do new things, it lets you do familiar things in new ways. As we're all developers, rather than going through the background information, this article just dives right in with an example and works from there. Here's a simple user interface—a typical window with a ListBox and a menu (see Figure 1).


 
Figure 1. Typical Simple Window: Here's a simple window example that contains a ListBox and a Delete menu.

In this example, you want to make sure that users have selected a customer from the list before they can click the Delete menu. How many times have you added this sort of code to one of your Windows applications?

   private void HandleMenus()
   {
      menuDelete.IsEnabled = lsbCustomers.SelectedItem != null;
   }
The code disables the Delete menu based on whether a user has selected a Customer. Code such as this is a common approach to keep menus and buttons enabled or disabled at the right times. Usually, calls to this or similar methods are riddled throughout the user interface like this:

   private void lsbCustomers_SelectionChanged(object sender, 
       SelectionChangedEventArgs e)
 
Figure 2. More Complex Window: Adding a second list box to the window example complicates the UI code for enabling or disabling the Delete menu.
{ HandleMenus(); }
This is a venerable approach, but it can easily become convoluted as your UI becomes more complicated. For example, suppose the window has two list boxes (see Figure 2).

Even though this still isn't a complicated window, simply adding one more list box requires even more conditional code to properly handle the enabling of the Delete menu.

   private void HandleMenus()
   {
      menuDelete.IsEnabled = 
         (lsbCustomers.SelectedItem != null && 
         ((ListBoxItem)lsbCustomers.SelectedItem).IsFocused) ||
         (lsbProducts.SelectedItem != null && 
         ((ListBoxItem)lsbProducts.SelectedItem).IsFocused);
                            
   }
It also requires additional code to determine which ListBox the delete operation should affect.

   private void menuDelete_Click(object sender, RoutedEventArgs e)
   {
      if (lsbCustomers.SelectedItem != null && 
         ((ListBoxItem)lsbCustomers.SelectedItem).IsFocused)
            lsbCustomers.Items.Remove(lsbCustomers.SelectedItem);
      else if (lsbProducts.SelectedItem != null && 
         ((ListBoxItem)lsbProducts.SelectedItem).IsFocused)
            lsbProducts.Items.Remove(lsbProducts.SelectedItem);
   }
Imagine a window with as few as five controls that support the Delete operation. The code necessary to wire up each one of those quickly becomes burdensome. This burden is exacerbated when more than one control can invoke the Delete operation. Fortunately, WPF provides a better approach. The WPF command framework simplifies the UI code required in situations like these because it separates commands from implementation, letting you bind controls to specific commands, as you'll see next.

  Next Page: Enter WPF Commands


Page 1: IntroductionPage 3: Routed Commands, Windows Events, and Custom Commands
Page 2: Enter WPF Commands 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES