locked
MVVM. Access TabControl->TabItems from ViewModel RRS feed

  • Question

  • Hi Folks,

     

    Well, I just start to use MVVM, and I want to migrate functionality from my old non mvvm project to my new mvvm project.

     

    The problem is the following one:

    In my old app (wizard application), I use tab control. And I have such things as States there. For this purpose I use StateMachine approach:

    My tab control looks like:

     

     private void sm_StateChanged(State current, State previous, State next)
        {
          switch (current.Name)
          {
            case StatesNames.WELCOME_SCREEN_STATE_NAME:
              MainTabControl.SelectedIndex = 0;
              break;
            case StatesNames.SERVER_SUMMARY_SCREEN_STATE_NAME:
              MainTabControl.SelectedIndex = 1;
              preCertificationTabControl.SelectedIndex = 0;
              break;
            case StatesNames.SERVER_DEFINITION_SCREEN_STATE_NAME:
              MainTabControl.SelectedIndex = 1;
              preCertificationTabControl.SelectedIndex = 1;
              break;
            case StatesNames.UPGRADE_PRECERTIFICATION_SCREEN_STATE_NAME:
              MainTabControl.SelectedIndex = 1;
              preCertificationTabControl.SelectedIndex = 2;
              break;
            case StatesNames.PRECERTIFICATION_SCREEN_STATE_NAME:
              NavigationStateMachine.GetInstance().SwitchToNextState();
              break;
          }      
        }
    

    Well, as you can see, I need to access my tabControl to indicate states :( but it is not correct from the MVVM point of view.

    Can anyone help me with workaround ??

     

    Thank you in advance,

     

    Julian. 

     

    Monday, August 30, 2010 3:28 PM

Answers

  • Hi Julian,

    Here is a simple sample:

    TabControl:

        <TabControl Name="MainTabControl" SelectedIndex="{Binding MainIndex}"/>
        <TabControl Name="PreCertificationTabControl" SelectedIndex="{Binding PreCertificationIndex}"/>
    

    ViewModel:

      public class ViewModel: INotifyPropertyChanged
      {
        protected void OnNotifyPropertyChanged(string p)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
          }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        int _MainIndex;
        public int MainIndex
        {
          get { return _MainIndex; }
          set
          {
            if (_MainIndex != value)
            {
              _MainIndex = value;
              OnNotifyPropertyChanged("MainIndex");
            }
          }
        }
    
        int _PreCertificationIndex;
        public int PreCertificationIndex
        {
          get { return _PreCertificationIndex; }
          set
          {
            if (_PreCertificationIndex != value)
            {
              _PreCertificationIndex = value;
              OnNotifyPropertyChanged("PreCertificationIndex");
            }
          }
        }
    
    
        private void sm_StateChanged(State current, State previous, State next)
        {
          switch (current.Name)
          {
            case StatesNames.WELCOME_SCREEN_STATE_NAME:
              MainIndex = 0;
              break;
            case StatesNames.SERVER_SUMMARY_SCREEN_STATE_NAME:
              MainIndex = 1;
              PreCertificationIndex = 0;
              break;
            case StatesNames.SERVER_DEFINITION_SCREEN_STATE_NAME:
              MainIndex= 1;
              PreCertificationIndex= 1;
              break;
            case StatesNames.UPGRADE_PreCertifICATION_SCREEN_STATE_NAME:
              MainIndex= 1;
              PreCertificationIndex= 2;
              break;
            case StatesNames.PreCertifICATION_SCREEN_STATE_NAME:
              NavigationStateMachine.GetInstance().SwitchToNextState();
              break;
          }
        }
      }
    

     

    Hope this helps,

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:17 AM
    Friday, September 3, 2010 3:13 AM
  • Why don't you add a couple of int properties MainTabControlSelectedTabIndex and PreCertificationTabControlSelectedTabIndex to your view model.  In the UI, bind the SelectedIndex property of each TabControl to the corresponding property.  Then, in the StateChanged method, just update these values instead of the TabControls directly.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:17 AM
    Monday, August 30, 2010 6:59 PM

All replies

  • Why don't you add a couple of int properties MainTabControlSelectedTabIndex and PreCertificationTabControlSelectedTabIndex to your view model.  In the UI, bind the SelectedIndex property of each TabControl to the corresponding property.  Then, in the StateChanged method, just update these values instead of the TabControls directly.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:17 AM
    Monday, August 30, 2010 6:59 PM
  • Thanks for replay, 

     

    Could you show small example with TabControl  (SelectedIndex={Binding Path = SelectedIndexProperty}) of View, and Property in ViewModel ?

     

    How it should be ?

     

    thanks you in advance,

    Julian

     

    Tuesday, August 31, 2010 9:10 AM
  • Hi Julian,

    Here is a simple sample:

    TabControl:

        <TabControl Name="MainTabControl" SelectedIndex="{Binding MainIndex}"/>
        <TabControl Name="PreCertificationTabControl" SelectedIndex="{Binding PreCertificationIndex}"/>
    

    ViewModel:

      public class ViewModel: INotifyPropertyChanged
      {
        protected void OnNotifyPropertyChanged(string p)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
          }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        int _MainIndex;
        public int MainIndex
        {
          get { return _MainIndex; }
          set
          {
            if (_MainIndex != value)
            {
              _MainIndex = value;
              OnNotifyPropertyChanged("MainIndex");
            }
          }
        }
    
        int _PreCertificationIndex;
        public int PreCertificationIndex
        {
          get { return _PreCertificationIndex; }
          set
          {
            if (_PreCertificationIndex != value)
            {
              _PreCertificationIndex = value;
              OnNotifyPropertyChanged("PreCertificationIndex");
            }
          }
        }
    
    
        private void sm_StateChanged(State current, State previous, State next)
        {
          switch (current.Name)
          {
            case StatesNames.WELCOME_SCREEN_STATE_NAME:
              MainIndex = 0;
              break;
            case StatesNames.SERVER_SUMMARY_SCREEN_STATE_NAME:
              MainIndex = 1;
              PreCertificationIndex = 0;
              break;
            case StatesNames.SERVER_DEFINITION_SCREEN_STATE_NAME:
              MainIndex= 1;
              PreCertificationIndex= 1;
              break;
            case StatesNames.UPGRADE_PreCertifICATION_SCREEN_STATE_NAME:
              MainIndex= 1;
              PreCertificationIndex= 2;
              break;
            case StatesNames.PreCertifICATION_SCREEN_STATE_NAME:
              NavigationStateMachine.GetInstance().SwitchToNextState();
              break;
          }
        }
      }
    

     

    Hope this helps,

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:17 AM
    Friday, September 3, 2010 3:13 AM
  • The TabItems should be a collection in the ViewModel, thus it would already have access to it. The View would be bound to said collection.
    Friday, September 3, 2010 4:15 AM