我正在使用棱鏡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也很愚蠢。我該如何改進?或者如果事件更適合我的話,我該如何實現禁用命令?