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?