Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap