3

I'm trying to learn MVVM and as a new person without a tutor, sometimes it gets sooo confusing. right now I have this problem:

** in one window, I have used 3 different views from different modelviews. ** each view is for selecting one item from a branch and we will traverse the tree. ** in first View, I choose my book ** the 2nd view will show pages of that book ** the last view I want it to show exercises of that page.

so far I was able to bind the 1st view to the Books. now, how should I bind the second view's source to the 1st one's slectedItem ?

MVVM Problem http://clickasun.ir/8283kitchen/images/mvvmproblem.jpg

Mohfath
  • 57
  • 1
  • 11
  • it would be great if someone can provide me with a link to a sample application. – Mohfath Aug 07 '12 at 14:59
  • 2
    This might go into your direction: http://stackoverflow.com/questions/4599802/concrete-examples-of-state-sharing-between-multiple-viewmodels-wpf-mvvm – Jens H Aug 07 '12 at 15:48

2 Answers2

1

there are several ways to achieve what you want. eg.

 public class Exercise{}

 public class Page 
 {
    public List<Exercise> MyExercise {get;set;}
 }

 public class Book
 {
    public List<Page > MyPages {get;set;}
 }

viewmodel

 public ObservableCollection<Book> MyBooks {get;set;}

xaml

 <ListBox x:Name=books ItemsSource="{Binding MyBooks}"/>
 <ListBox x:Name=pages ItemsSource="{Binding ElementName=books,Path=SelectedItem}"/>
 <ListBox ItemsSource="{Binding ElementName=pages,Path=SelectedItem}"/>

this is of course just one way to do it.

ps: code handwritten, so check for errors

blindmeis
  • 22,175
  • 7
  • 55
  • 74
0

OK I want to thank anyone who read this and tried to help, Specially yo guys "Blindmeis, Jen H"

thank you.

I know this scenario had to be so general, I just couldnt find it.

so I found 2 approaches to it that both can be good solutions.

  1. Use Microsoft Prism. (Its a pattern to make modular programs in WPF with ease) MSDN LINK Another LINK

  2. Use Master Detail Pattern Here it is clear

So I got mine solved. Hope it would be useful for you too.

Mohfath
  • 57
  • 1
  • 11