How to preserve the full state of the View when navigating between Views in an MVVM application?

后端 未结 5 846
甜味超标
甜味超标 2020-12-05 16:09

I have an MVVM application that requires basic backward/forward navigation between screens. Currently, I have implemented this using a WorkspaceHostViewModel that tracks the

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 16:34

    I have managed to fix it without using TabControlEx (cause it didn't work for me either). I've used Datatemplates and templateselector in order to switch between tabs.

    Xaml:

     
        
        
            
        
        
            
        
    
            
            
    
                
    
                
            
                
                        
                
            
        
    

    The DataTemplateSelector:

     public class MainTabViewDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;
            switch ((item as TabInfoEntity).TabIndex)
            {
                case 1:
                    {
                        return element.FindResource("Dashboard") as DataTemplate;
                    }
                case 2:
                    {
                        return element.FindResource("SystemHealth") as DataTemplate;
                    }
    
            }
            return null;
        }
    }
    

    TabInfoEntity class (list of objects of this type are the itemsource of the TabControl):

    public class TabInfoEntity { public TabInfoEntity() {

        }
        private string name;
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        private int tabindex;
    
        public int TabIndex
        {
            get { return tabindex; }
            set { tabindex = value; }
        }
    }
    

提交回复
热议问题