I have been trying to do this for ages and having no joy whatsoever.
I have a ribbon window of the following hierarchy:
- MainWindow
- MainContent (Tab Control)
- TabContainerViewModel
- ViewModelBase
- TabContainerViewModel
- MainContent (Tab Control)
View model base has an ObservableCollection of tabs type ViewModelBase. The Tab Control itself is binding fine to these, displaying appropriate view models. I have 2 problems however, I want a “NotepadView” (Of type viewmodelbase) to be replicated numerous times (one view many view models).
At the minute, I have 4 views (NotePadViewModelx where x is 1-4) with corresponding viewmodels, this is because each view had the same text per tab. Now I have individual views per tab.
When I type into tab 1 and switch to tab 2, I lose what I typed in tab one when I click back.
Could somebody help to provide me with a solution to these problems?
- One NotepadView for multiple instances of the ViewModel
- Keeping information when tabs change
Many thanks in advance.
I moved the Header and Text properties into ViewModelBase and then when I add a ViewModelBase as a Tab I used a DataTemplate for the ViewModelBase as I was for the NotePadViewModels with just a textbox.
As apose to using this:
Thanks to Rachel for a small hint to the problem. Basically on the previous way nothing was actually binding within the datatemplate so it had no reason to keep it as a resource. By adding a default textbox within a datatemplate I can add as many notepads as I want whilst being able to add other views which don’t need a textbox.