ChaseTheSun
ChaseTheSun

Reputation: 3840

WPF MVVM guidance for beginner

Trying to make a document viewer, like Acrobat, which consists of pages, and each page consists of thumbnail images (of varying sizes at users discretion). I’m not sure how to design the MVVM. At the moment I have:

DocumentViewModel, PageViewModel, ThumbnailViewModel

DocumentViewModel has ObservableCollection() // keeps track of Pages

PageViewModel has ObservableCollection() // keeps track of thumbnails to be displayed by document.

However this design causes problems. For example, if there are 3 pages of thumbnails. And half of the thumbnails are removed from page 2, then page 3 thumbnails should automatically relocate to the newly available space on page 2. Each PageViewModel would have to have awareness about each other, which would be breaking the MVVM policies, no? Would it be better to have the 2 ObservableCollections, declared within DocumentViewModel?

Any help, pointers, advice, guidance welcomed.

Upvotes: 0

Views: 337

Answers (3)

ChaseTheSun
ChaseTheSun

Reputation: 3840

Solved! i'm using both helgeheldre's suggestion of using the DocumentViewModel as the controller and Jen H's suggestion of using Caliburn.Micro's Framework for messaging.

All messages get routed to the DocumentViewModel (Via EventAgregator), who then delegates tasks to individual PageViewModels.

Upvotes: 0

Jens H
Jens H

Reputation: 4632

The ViewModels do not need to be aware of each other.

I suggest using any MVVM framework of your choice, for example Caliburn Micro. CM allows you to send messages between objects without them knowing each other and keep them completely decoupled.

Simply put, the Caliburn Micro framework offers an EventAgregator to dispatch the messages. The objects that need to send a message register the message type with the EventAggregator and the objects that need to receive subscribe for them also at the EventAggregator.

See an example of this here:Introduction to messaging with Caliburn.Micro’s EventAggregator.

Upvotes: 1

helgeheldre
helgeheldre

Reputation: 1101

The PageViewModels should not be aware of each-other.

DocumentViewModel will be aware of the PageViewModels you have and it can handle redeistribution of ThumbnailViewModels. Let the DocumentViewModel handle number of pages and ThumbNailViewModels per PageViewModel.

If you move the ObservableCollection of Thumbnails out to the DocumentViewModel you can dynamically create your PageViewModels based on need. Whenever the ThumbnailViewModel collection changes you will get notifications and you can then alter the PageViewModels based on that.

Upvotes: 1

Related Questions