Changing View on parent viewmodel from child viewmodel

722 views Asked by At

Using Galasoft Mvvmtoolkit, I implemented mvvm in wpf. Now I have created 3 view and 1 ViewModel for each view. Below is my sample code.

//For Main.xaml
public MainViewModel:ViewModelBase
{ 
     ViewModelBase CurrentView{get;set;}

     public MainViewModel(){
         CurrentViewModel = new InfoViewModel();
     }
}

//For Info.xaml
public InfoViewModel{
     //Open DetailViewMode by setting 
     //CurrentViewModel property to an instance of DetailViewModel
     ICommand ShowDetailCommand; 

     public InfoViewModel(){
          ShowDetailCommand = new RelayCommand(()=>{
             //CurrentViewModel= new DetailViewModel();
          })
     }
}

//For Detail.xaml
public DetailViewModel{

}

I want to show Detail.xaml usercontrol in Main.xaml which has a ContentControl in it binded to CurrentViewModel. On load, I am loading Info.xaml in it and want to show Detail.xaml when user clicks on a button present in Info.xaml

1

There are 1 answers

2
WPFUser On

below is the codes, which worked for me once, i haven't used any toolkit. and I haven't tested the code.

And OnClick method implemented with Command, and you can use CommandParameter to decide which viewmodel you have to set as currentviewmodel.

public class ViewModelLocator : ViewModelBase
{
    private static RegularViewModel _regularViewModel;

    public static RegularViewModel RegularViewModel
    {
        get
        {
            if (_regularViewModel == null)
            {
                _regularViewModel = new RegularViewModel(MainViewModel);
            }
            return
                _regularViewModel;
        }
        set { _regularViewModel = value; }
    }

    private static AdvancedViewModel _advancedViewModel;

    public static AdvancedViewModel AdvancedViewModel
    {
        get
        {
            if (_advancedViewModel == null)
            {
                _advancedViewModel = new AdvancedViewModel(MainViewModel);
            }
            return
                _advancedViewModel;
        }
        set { _advancedViewModel = value; }
    }

    private static MainViewModel _mainViewModel;

    public static MainViewModel MainViewModel
    {
        get
        {
            if (_mainViewModel == null)
            {
                _mainViewModel = new MainViewModel();
                _mainViewModel.ViewModels.Add(RegularViewModel);
                _mainViewModel.ViewModels.Add(AdvancedViewModel);
            }
            return
                _mainViewModel;
        }
        set { _mainViewModel = value; }
    }


}

public class MainViewModel : ViewModelBase, INaviagte
{

    private ObservableCollection<ViewModelBase> viewModels;

    public ObservableCollection<ViewModelBase> ViewModels
    {
        get { return viewModels; }
        set { viewModels = value; }
    }

    private ViewModelBase selectedViewModel;

    public ViewModelBase SelectedViewModel
    {
        get { return selectedViewModel; }
        set { selectedViewModel = value; }
    }
    public MainViewModel()
    {
        ViewModels = new ObservableCollection<ViewModelBase>();
    }

    private bool showCompactView;

    public bool ShowCompactView
    {
        get { return showCompactView; }
        set { showCompactView = value;RaisePropertyChanged("ShowCompactView"); }
    }

    public void Navigate(ViewModelBase _viewModel)
    {
        SelectedViewModel = _viewModel;
    }

}

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    internal void RaisePropertyChanged(string v)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(v));
    }

}
public class RegularViewModel : ViewModelBase
{
    INaviagte mainViewModel;
    public RegularViewModel(INaviagte _mainViewModel)
    {
        mainViewModel = _mainViewModel;
    }
    public RegularViewModel()
    {

    }
    private void OnClick(object obj)
    {
        mainViewModel.Navigate(ViewModelLocator.AdvancedViewModel);
    }
}
public class AdvancedViewModel : ViewModelBase
{
    INaviagte mainViewModel;
    public AdvancedViewModel(INaviagte _mainViewModel)
    {
        mainViewModel = _mainViewModel;
    }
    public AdvancedViewModel()
    {

    }

    private void OnClick(object obj)
    {
        mainViewModel.Navigate(ViewModelLocator.RegularViewModel);
    }
}

public interface INaviagte
{
    void Navigate(ViewModelBase _viewModel);
}