A little unsure about how to use Command system

I've read several articles on the Command system WPF gives developers. While I undestand basic uses for this structure, I'm a little confused on how to implement it on a particular senario:

I'm building a basic finance application to keep track of transactions, budgeting, etc... I'm using a basic NavigationWindow with Pages for different areas of the application.

I have a page, Accounts.xaml, which lists all the accounts set up in a ListView. I want a "Command" way of allowing a user to select an account and navigate to Transactions.xaml, which lists all the transactions for that account.

However, I want to use this same command on other pages. For example, let's say I'm on a page, Reports.xaml, which shows a basic report of a list of accounts. Let's say on this report it says "This report reflects transactions from [Account 1] and [Account 2]", where [Account 1] and [Account 2] are the names of accounts. I want the user to be able to click on these [Account 1] and [Account 2] links which takes them back to the Transactions.xaml, listing the transactions that account has.

So, I guess I have a few questions:

  1. Is this a senario for using one static command E.g. a command defined globablly (statically) in the application which many buttons are linked to
  2. Where would I define this command
  3. Is it possible to have unique IsEnabled return values for different areas this command may be used For example, on the Accounts.xaml page, there will be three different ways to "select" an account and see it's transactions. First, the user can click the name of the account in the ListView. Second, the user can double click a ListViewItem. Thirdly, there will be a button at the bottom of the page saying "Next", which will take the selected ListViewItem and go from there. As you can see, I need separate IsEnabled properties. It should always return true on the first two ways I mentioned. On the third way, the button at the bottom way, I want IsEnabled to check if the ListView has a selected item. Again, this gets even more complicated when I try to use the command on another page (Reports.xaml).

Whew. That's a lot to type. Does anyone have any good advise or answers

Thanks,
Johann



Answer this question

A little unsure about how to use Command system

  • D.Hamilton

    1. Where a command is accessible from depends how you to find it in code. There is no Application.CommandBindings collection.

    2. Defining a command is just a matter of instantiating a command (often RoutedUICommand), setting a few properties, and if you want to use it from xaml, assigning it to a public static field.

    3. Yes, but... yes, there's a notion of parameterized commands. In February CTP you can say for example <Button Command="mycommand" CommandParameter="myparameter"/>

    The "but" is about using it to solve the scenario you described. The key constraint I heard was the command is parameterized based off a list view's selected item. That the parameter is going to change makes it more difficult than if the parameter was always the same value. I think you'll run into problems if you try to combine data binding with parameterized commands -- Button.CommandParameter is a dependency property, but InputGesture.CommandParameter is not, so doesn't support data binding.

    Given the scenario you described, I'd probably define a GotoAccount(account) method, and then define three separate commands -- GotoNextAccount, GotoSelectedAccount, and GotoAccount (which takes an account parameter). That is, assuming you have more than one piece of UI invoking each command (e.g., buttons, menus, shortcut keys), if you don't there's not a lot of value packaging the logic into a command and it's easier just to do a plain old button click handler. It's all about how much reuse you actually get by things into commands.



  • A little unsure about how to use Command system