views:

436

answers:

1

Hy guys!

I am currently working on a little WPF project using MVVM via the Onyx framework.

My currentview architecture is like this:

<DockPanel>
    <Menu DockPanel.Dock="Top" Background="#cecece">
        <!-- Menu -->
    </Menu>

    <Grid>
        <views:TranslationView x:Name="translationView" />
    </Grid>
</DockPanel>

The question that I now have is how to implement the relationship between the two viewmodels. I currently basically just have the TranslationView have its own ViewModel with no link to the parent ViewModel.

The problem is that I want to be able to open some file via the MainView and then parse the contents and display them in the TranslationView. Is there a recommended way to do this?

I thought about just using the TranslationViewModel as a property in the MainViewModel and then using it as DataContext for the TranslationView but it kinda seems to run against Onyx's model to define the ViewModel through a type (not a object) reference.

A: 

Onyx doesn't have a "model to define the ViewModel through a type (not a object) reference". The ViewModel attached property can be assigned an object reference. In fact, this property is an Object type and uses coercion to change a Type instance into an object instance of the specified Type. This is a convenience only. You're free to just assign an object reference created in any way you want.

Like most questions, there's any number of ways to solve your problem. The solution you mention but dismissed because you thought it went against Onyx's design is one that could work, but I'd hesitate to use, simply because it creates tighter coupling. Another solution would be to utilize the Event Agregator pattern to communicate between the views in a disconnected fashion. Or you could utilize a more service oriented approach. For example, I usually define an IApplication service that stands in where you'd normally access Application.Current in a tightly coupled design. You could provide a property on this service to hold the contents of the loaded file, and expose INotifyPropertyChanged on the service to allow the TranslationViewModel to know that the property was changed.

wekempf