我正在使用棱镜2,试图在模块使用的shell中添加四个导航按钮(第一条记录,最后一条记录,上一条记录,下一条记录)。我还希望这些按钮被禁用,如果活动View/ViewModel不提供这些功能。由模块共享的棱镜壳按钮
我尝试使用事件,但不知道如何实现我关于禁用按钮的第二个目标。看来我需要检查当前的活动View/ViewModel,看他们是否在View切换期间订阅了点击事件。但我认为出版商应该不知道用户...
不知何故,我尝试了我自己的方式。我创建了一个IDocNavigation接口,其中有四个方法与我的四个按钮相对应。在运行时,我检查模块的ViewModel是否实现了该接口,并更改ICommand。以下是我的代码。我只包含一个LastRecordCommand:
public ShellViewModel(Views.Shell shell)
{
this.Shell = shell;
shell.DataContext = this;
shell.MainDocking.ActivePaneChanged += (s, e) =>
{
if (e.NewPane.Content is UserControl &&
((UserControl)e.NewPane.Content).DataContext is IDocumentNavigate)
{
IDocumentNavigate vm = ((UserControl)e.NewPane.Content).DataContext as IDocumentNavigate;
LastRecordCommand = new RelayCommand(x => vm.GotoLastRecord(), x => true);
}
else
{
LastRecordCommand = new RelayCommand(x => { }, x => false);
}
};
//...
我觉得这些都很难看。创建一个空的RelayCommand也很愚蠢。我该如何改进?或者如果事件更适合我的话,我该如何实现禁用命令?