WPF MVVM: Binding a different ViewModel to each TabItem?

前端 未结 5 1135
庸人自扰
庸人自扰 2020-12-28 17:22

I have a main window with a tab control containing 2 tabItems:

\"Main

I currently

5条回答
  •  甜味超标
    2020-12-28 17:51

    No sure why the selected answer is suggesting to use ObservableCollection and create a collection of tabs and then reference them by index.

    I find it more clean to do it this way:

    MainViewModel tab1= ConfigViewModel Tab2 = UserProfileViewModel

    in MainViewModel:

    private ConfigViewModels _configVM;
    public ConfigViewModels ConfigVM { get { return _configVM; } }
    
    private UserProfileViewModel _userProfileVM;
    public UserProfileViewModel UserProfileVM { get { return _userProfileVM; } }
    
    public MainViewModel(){
    
    _configVM = new ConfigViewModels();
    _userProfileVM = new UserProfileViewModel ();
    }
    

    In th XAML file:

    
    
    
    

    I'm not sure I understand the benefit of doing this through an observable collection.

提交回复
热议问题