使用Galasoft Mvvmtoolkit,我在wpf中实现了mvvm。现在我为每个视图创建了3个视图和1个ViewModel。以下是我的示例代码。
//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{
}
我想在Main.xaml中显示Detail.xaml usercontrol,其中ContentControl绑定到CurrentViewModel。加载时,我在其中加载Info.xaml,并希望在用户单击Info.xaml中的按钮时显示Detail.xaml
答案 0 :(得分:0)
下面是代码,这对我有用,我没有使用过任何工具包。我还没有测试过代码。
使用Command实现的OnClick方法,您可以使用CommandParameter来决定必须将哪个视图模型设置为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);
}