【发布时间】:2017-07-09 09:27:37
【问题描述】:
我的主窗口有一个侧边栏菜单。当单击菜单上的某个项目时,我将在 ContentControl 上呈现该项目的页面 (UserControl)。这是它的样子。
我的主视图模型
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);
}
渲染部分我的MainView
<ContentControl Content="{Binding ElementName=lstSystemMenu, Path=SelectedItem.Content}" />
我的主要问题是我只是在我的 MainView 上呈现内容,而没有实际调用或绑定它的 ViewModel。
我确信我的 MVVM 框架的实现有问题。请告诉我我哪里出了问题以及实现这一点的最佳方法是什么。
【问题讨论】:
-
Dashboard,Reports,Configuration... 是什么类型?它们是其他视图模型,还是 UI 元素?如果你想遵循 MVVM 模式,那么 viewmodel 不应该有任何 UI 元素。事实上,我认为系统菜单应该是视图层的一部分,或者至少是一个服务 -
这些是视图 (UserControl)。您能帮我在单击菜单项时如何显示这些视图,而不像我目前那样做吗?哦,什么是服务?
-
如果没有可靠地重现问题的良好minimal reproducible example 以及对问题的更准确描述,则无法编写此问题的正确答案。在您阅读介绍性信息(包括Tour 和Help Center 中有关Asking 的其他文章之前,请不要向 Stack Overflow 发布问题