0

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

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • In similar scenario, [this](http://stackoverflow.com/a/19654812/2819451) answer helped alot on understanding the concept. – Gopichandar Dec 19 '16 at 05:32

1 Answers1

0

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);
}
WPFUser
  • 1,145
  • 7
  • 24
  • Are you binding this OnClick to some button? – Shantanu Gupta Dec 19 '16 at 04:49
  • yes, just saying that, you can implement like that. I will implement CanClick and Click method for ClickCommand which is DelegateCommand, and will bind that ClickCommand to Button. – WPFUser Dec 19 '16 at 04:54