0

I´m learning the MVVM for WPF. I know how to rout events and split the Code in View, ViewModel, Model. I have in my main-XAML a TabControl and have split the tabs into different views. My Question is, how can i pass an object from one class to another? (From the MainWindow.cs to the SubWindow.cs)

MainWindowRessources XAML:

....
<DataTemplate DataType="{x:Type vm:SubWindow}">
  <vw:SubWindow />
</DataTemplate>

<vm:SubWindow x:Key="subView" />
..

MainWindow XAML:

<Window.Resources>
  <ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>

...
..
<TabItem>
        <ContentControl Content="{StaticResource subView}" />
</TabItem>
...
..
jwillmer
  • 3,570
  • 5
  • 37
  • 73
  • 3
    It is Model, View and ViewModel. There is no ModelView. You probably need to get your concepts right. If your concepts are wrong, then your code is wrong. – Stephen Chung Apr 07 '11 at 08:20
  • Pass an object from the MainViewModel to SubViewModel, using a constructor or a property setter. – vortexwolf Apr 07 '11 at 08:22
  • MVVM means never having to say "mainwindow.cs". –  Apr 07 '11 at 14:00

2 Answers2

2

You should consider implementing the mediator pattern to allow your view models to communicate with each other.

See this Stackoverflow answer for more information.

Community
  • 1
  • 1
Oppositional
  • 11,141
  • 6
  • 50
  • 63
0

Depending on the context/use, you could create a DependencyProperty on the SubWindow class, and pass the object as a parameter, ala <vm:SubWindow MyNewProperty="some-value-or-object here"/>.

Info about creating DependencyProperties: http://msdn.microsoft.com/en-us/library/ms752914.aspx

What is the object you're wanting to pass?

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144