2012-03-06 32 views
0

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

回答

0

您可以在棱鏡中使用CompositeCommand。

定義可用CompositeCommand

public static readonly CompositeCommand FirstRecord= new CompositeCommand(true); 

然後,在全球範圍內的你的模塊視圖模型

class Module1 
    { 


     public DelegateCommand Module1Firstrecord{ get; set; } 
     Module1() 
     { 
      Module1Firstrecord = new DelegateCommand(this.FirstRecord, CanExecute); 
     } 

     private void FirstRecord() 
     { 
     //do whatever you want 

     } 

     private bool CanExecute() 
     { 
       return true; 
     } 

     private void Module1_IsActiveChanged(object sender, EventArgs e) 
     { 
     //Find if your window is acive 
     // if it is active Module1Firstrecord.IsActive = true  
     //else false. 
     } 
} 

隨着IActiveAware您可以輕鬆地處理活動窗口的場景。根據您的活動模塊是否具有該命令的處理程序,而不是該按鈕將啓用/禁用。