开发者

Silverlight Commands MVVM

I'd like to 'reduce' the number of commands in my ViewModel class. I have one ViewModel which contains 5+ lists (using Listboxes on View to presentate, and I'm binding an ObservableCollection to its ItemSource parameter; also binding the SelectedItem property), and each list should have its own Add/Remove/etc button.

So, it looks like this:

public class PersonViewModel : ViewModelBase
{
    Person _Per开发者_JS百科son;
    private ObservableCollection<WorkPlaceViewModel> _WPlaces;
    private ObservableCollection<LanguageViewModel> _Languages;
    ... other lists

    private WorkPlaceViewModel _SelectedWorkPlaceView;
    ...

}

Adding 5x2 command makes the ViewModel a bit large. I could hardcode a string to the CommandParameter and handle it with a switch-case in my ViewModel but this sounds like hacking. :p Or should I go with creating a Remove command for each listbox, and pass the SelectedItem as a parameter?

Whats the best way for this in MVVM?


Why do you have a collection of viewmodels in your viewmodel? What is the PersonViewModel supposed to accomplish. It sounds to me like you need to break up your view into multiple views and bind them to different viewmodels. Your views and viewmodels should be as small as possible to reduce coupling.

That said, I would recommend commands for add/remove on each observable collection. Using a hardcode string is messy, as you guessed. If each observable collection has their own add/remove that will allow different logic for each collection and is more maintainable.

Can you post more code from your ViewModel and describe the app a bit? That will help us determine if there could be a better design decision.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜