1

I have my WPF project set up as follows

In my MainWindow I have some tabs. A SearchJob tab and an Edit Job tab, the tabs display their own respective user controls

They all have their own ViewModels as their DataContext

MainWindow - MainWindowVM SearchJobs - SearchJobsVM EditJob - EditJobVM 

After I search for jobs I get a grid back that is bound to an ObservableCollection of Job objects

When I double click the results grid I want to make the Edit tab visible passing it's view model the id of the row I double clicked on

I also want to make some of the tabs in my MainWindow invisible. The tabs are bound to Visibility properties in my MainWindowVM

I am able to get the id of the row I double clicked on

My question is that from the SearchJobsVm I need to access bot the MainWindowVM to set the Visibility properties and also access the EditJobVM to set the ID

How do I access the DataContext (the view models) of the MainWindowVM and EditJobVM from SearcvhJobVM?

In Mainwindow I set the DataContext like so:-

<Window.DataContext> <vm:MainWindowViewModel /> </Window.DataContext> 

and the user controls are added in the xaml like so

<TabItem Header="Search"> <Grid Background="#FFE5E5E5"> <uc:SearchJobView></uc:SearchJobView> </Grid> </TabItem> 

My DataContext for SearchJobView is set like:-

<UserControl.DataContext> <vm:SearchJobViewModel/> </UserControl.DataContext> 

My DataContext for EditJobView is set like:-

<UserControl.DataContext> <vm:JobViewModel/> </UserControl.DataContext> 
2
  • How the view models are instantiated and bound to user controls? add some code to explain. Commented Oct 8, 2014 at 4:35
  • Have update my listing Commented Oct 8, 2014 at 5:09

1 Answer 1

1

the simple way would be that the MainVM hold both instances of your SearchJobsVM and EditJobVM. now the MainVM can simply handle all stuff.

eg the SearchJobVM expose an event for your doubleclick stuff. the MainVM subscribe to this event and give the Id from the eventargs to the EditJobVM and set the current workspace to the EditJobVM.

EDIT. i would use DataTemplates for your child vms and a contentPresenter in the MainView. but you can also use a TabControl and set Visibility

<DataTemplate DataType="{x:Type local:EditJobVM}"> <uc:EditJobUsercontrol/> </DataTemplate> 

MainView

 <ContentPresenter Content="{Biinding 'Workspace}"/> 

MainVM

 public object WorkSpace {get;set;} this.Workspace = this._myInstanceOfEditJobVM; //now the EditJobView is shown in the contentpresenter 
Sign up to request clarification or add additional context in comments.

4 Comments

I initially went down the path of having the MainVM have an instance of both the other VM's in code as you suggest but I found that the Constructor of the other VM's was being called twice, one for the instance I was instantiating and once because it was being created by the XAML. Hence I ended up with two threads. Will this approach rectify that?
if it is called twice then you call it twice. you should look at your code why this happen. i would suggest it has to do with <UserControl.DataContext>.
Ok - I'll give your suggestion a go and report back
Yes you are right. Called twice because I was trying to set the DataContext in the xaml of the child user control. Removed it from there and set it to the Property exposed from the MainVM like so <uc:SearchJobView DataContext="{Binding SearchJobVM}"></uc:SearchJobView>. Worked a treat, thanks. Will look to implement your ContentPresenter suggestion. Thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.