如何动态更改用户控件在wpf MVVM指示灯中的usercontrol中出现的按钮(单击)
我有一个主窗口,将Usercontrol作为ContentControl主机托管。我想要的是,动态更改按钮单击(存在于第一个Usercontrol中)的usercontrol到另一个usercontrol。
目前,我已在主窗口资源中创建了一个DataTemplate,该资源由usercontrol的相应ViewModel组成
我想在view1中点击按钮时从View1更改为view2。 那么我应该如何在ViewModel1(US1 viewModel)中更改为US2
我目前正在研究MVVM光。
我有一个服务定位器,它具有每个VM的注册实例。 问题是如何指向VM1中的VM2实例。
欢迎任何帮助!!!!!
将您的Window视为shell并使用MvvmLight的Messenger将消息发送到您的shell以交换视图。
例如:
MainWindow.xaml
MainWindowViewModel.cs
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace WpfApplication1 { public class MainWindowViewModel : ViewModelBase { private FrameworkElement _contentControlView; public FrameworkElement ContentControlView { get { return _contentControlView; } set { _contentControlView = value; RaisePropertyChanged("ContentControlView"); } } public MainWindowViewModel() { Messenger.Default.Register(this, (switchViewMessage) => { SwitchView(switchViewMessage.ViewName); }); } public ICommand ChangeFirstViewCommand { get { return new RelayCommand(() => { SwitchView("FirstView"); }); } } public ICommand ChangeSecondViewCommand { get { return new RelayCommand(() => { SwitchView("SecondView"); }); } } public void SwitchView(string viewName) { switch (viewName) { case "FirstView": ContentControlView = new FirstView(); ContentControlView.DataContext = new FirstViewModel() { Text = "This is the first View" }; break; default: ContentControlView = new SecondView(); ContentControlView.DataContext = new SecondViewModel() { Text = "This is the second View" }; break; } } } }
FirstView.xaml
FirstViewModel.cs
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WpfApplication1 { public class FirstViewModel : ViewModelBase { private string _text; public string Text { get { return _text; } set { _text = value; RaisePropertyChanged("Text"); } } public ICommand ChangeToSecondViewCommand { get { return new RelayCommand(() => { Messenger.Default.Send(new SwitchViewMessage { ViewName = "SecondView" }); }); } } } }
SecondView.xaml
SecondViewModel.cs
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WpfApplication1 { public class SecondViewModel : ViewModelBase { private string _text; public string Text { get { return _text; } set { _text = value; RaisePropertyChanged("Text"); } } public ICommand ChangeToFirstViewCommand { get { return new RelayCommand(() => { Messenger.Default.Send(new SwitchViewMessage { ViewName = "FirstView" }); }); } } } }
SwitchViewMessage.cs
namespace WpfApplication1 { public class SwitchViewMessage { public string ViewName { get; set; } } }
在MainViewModel中创建一个属性,例如“DisplayViewModel”,其中包含vm1和vm2的基本类型。
private MyViewModelBase _displayViewModel; public MyViewModelBase DisplayViewModel { get { return _displayViewModel; } set { _displayViewModel = value; OnPropertyChanged("DisplayViewModel"); // Raise PropertyChanged } }
在MainView.xaml中插入一个绑定到DisplayViewModelProperty的ContentControl:
在Button-Command上,您可以使用另一个ViewModel通过Setter修改DisplayProperty,并结合DataTemplates,ContentControl显示的UserControl应根据新设置的ViewModel-Type更改为View。
private void MyButtonCommand() { DisplayViewModel = new ViewModel2(); }