Prism Shell按钮由模块共享

时间:2012-03-06 17:03:12

标签: prism

我正在使用Prism 2,尝试在shell中添加四个导航按钮(First Record,Last Record,Previous Record,Next Record)以供模块使用。如果活动的View / ViewModel不提供这些功能,我也希望禁用这些按钮。

我尝试使用事件,但不知道如何实现关于禁用按钮的第二个目标。我似乎需要检查当前活动的View / ViewModel,看看他们是否在View切换期间订阅了click事件。但我认为出版商应该不知道用户......

不知怎的,我尝试了自己的方式。我创建了一个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也很愚蠢。我该如何改进?或者如果事件在我的情况下更合适,我怎样才能实现禁用命令?

1 个答案:

答案 0 :(得分: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,您可以轻松处理活动窗口场景。根据您的活动模块是否具有命令处理程序而不是按钮将启用/禁用。

相关问题