我的主窗口有一個側邊欄菜單。當單擊菜單上的項目時,我將在ContentControl上呈現該項目的頁面(UserControl)。這是它的樣子。將ViewModel綁定到CustomControl
我MainViewModel
public MainViewModel()
{
SystemMenu = new List<SystemMenuViewModel>();
SystemMenu.Add(new SystemMenuViewModel("Dashboard", new Dashboard()));
SystemMenu.Add(new SystemMenuViewModel("Appointments", new Dashboard()));
SystemMenu.Add(new SystemMenuViewModel("Reports", new Reports()));
SystemMenu.Add(new SystemMenuViewModel("Configuration", new Configuration()));
}
private string _windowTitle = GlobalVariables.WindowTitleDefault;
private string _currentPage = "Dashboard";
public string WindowTitle
{
get { return _windowTitle; }
set
{
_windowTitle = value;
NotifyOfPropertyChange(() => WindowTitle);
}
}
public string CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
NotifyOfPropertyChange(() => CurrentPage);
}
}
public List<SystemMenuViewModel> SystemMenu { get; set; }
我SystemMenuViewModel
private string _name;
private object _content;
public SystemMenuViewModel(string name, object content)
{
_name = name;
Content = content;
}
public string Name
{
get { return _name; }
set { this.MutateVerbose(ref _name, value, RaisePropertyChanged()); }
}
public object Content
{
get { return _content; }
set { this.MutateVerbose(ref _content, value, RaisePropertyChanged()); }
}
public event PropertyChangedEventHandler PropertyChanged;
private Action<PropertyChangedEventArgs> RaisePropertyChanged()
{
return args => PropertyChanged?.Invoke(this, args);
}
我在渲染部分
<ContentControl Content="{Binding ElementName=lstSystemMenu, Path=SelectedItem.Content}" />
我的主要問題的MainView是,我只是呈現在我的MainView的內容,也不得實際調用或綁定其ViewModel。
我確信在我的MVVM框架的實現中有些問題。請告訴我我出錯的部分是什麼,以及實施這一部分的最佳方式是什麼。
什麼類型。 'Dashboard','Reports','Configuration' ...?他們是其他視圖模型,還是他們的UI元素?如果你想遵循MVVM模式,那麼視圖模型不應該有任何UI元素。相信系統菜單應該是視圖層的一部分,或者至少是一個服務 –
這些是視圖(UserControl)。你可以幫助我如何顯示這些視圖,當一個菜單項被點擊時沒有按照我現在做的方式?哦,什麼是服務? – manoyanx
沒有好的[mcve]能夠可靠地再現問題並更準確地描述問題,因此無法寫出對此問題的正確答案。請不要將問題發佈到Stack Overflow,直到您閱讀介紹性信息,包括[Tour](https://stackoverflow.com/tour)和其他關於[Asking]的文章(https://stackoverflow.com/幫助/詢問)在[幫助中心](https://stackoverflow.com/help) –