2

I am working with MVVM and encounter the following problem with the use of many similar commands.

I wanna make many buttons which is used to show the books of different categories.

The model is some data about a book, say,

class book 
{
    string name;
    string author;
    .....
    .....
    string category;
}

A list of book is stored. A GUI is used for the user to view the books with different categories. Therefore, buttons for each category is created. For example:

Buttons:

Comic
Social
Academic
Science
Leisure
Political
Cartoon
Gambling
Magazine
All

There are about 10 buttons that I have to created. (9 categories, 1 for viewing all)

In MVVM, each button has its own command. From that, there would be 10 commands in the ViewModel. However, the coding 9 of its are very similar with the only difference in the category.

So, I would like to ask if there is any suggestion/methods to make it more code-saving?

I do have an idea. That is to combine all the commands into one. However, this method required the View (Xaml) to pass a CommandParameter in each button to the one command in order to distinguish the different category. This will make the XAML more complex and increase coupling level with the ViewModels/Business logic.

So, would there are more concise way?

user883434
  • 717
  • 2
  • 10
  • 25
  • 1
    I don't understand your statement about using a CommandParameter... how does it make the XAML more complex or increase coupling? It's just an extra property on the button, and it makes your ViewModel much more concise. – Thomas Levesque Nov 17 '11 at 09:25
  • I agree with Thomas Levesque, passing in the CommandParameter makes much more sense. Your VM will be a lot lighter and the additional XAML isn't a concern. – Fermin Nov 17 '11 at 09:27
  • I recommend refactoring your UI design. Use a combo box to select the genre before hitting the button, or put each genre on a separate tab page (though you could share the body implementation for the pages themselves). – Merlyn Morgan-Graham Nov 17 '11 at 14:35

1 Answers1

2

You can use DelegateCommand from Microsoft.Practices.Composite.Presentation.dll and use CommandParameter

    <Button Command="{Binding bookCmd}"  CommandParameter="Comic">
    <Button Command="{Binding bookCmd}"  CommandParameter="Music">
    <Button Command="{Binding bookCmd}"  CommandParameter="News">


private DelegateCommand<string> _bookCmd;

_bookCmd= new DelegateCommand<string>(ExecuteBookCmd);

private void ExecuteBookCmd(string category)
{
 //Have fun
}
Steven Muhr
  • 3,339
  • 28
  • 46
  • Because the DelegateCommand object is in Microsoft.Practices.Composite.Presentation.dll – Steven Muhr Nov 17 '11 at 12:31
  • 1
    Right but you can easily add it into your solution without PRISM (see [this SO post](http://stackoverflow.com/questions/924978/is-a-delegatecommand-the-same-as-an-attached-behavior)), anyway +1 for the good idea – sll Nov 17 '11 at 14:30
  • The implementation of ICommand is trivial, so there's no need to use Prism. –  Nov 17 '11 at 15:10