简体   繁体   English

如何通过使用MVVM在Prism(+ MEF)视图中通过特定控件的可用性来控制WPF 4.5 Ribbons上下文选项卡的可见性?

[英]How do I control a WPF 4.5 Ribbons contextual tab visibility by availability of a particular control within a Prism (+ MEF) view using MVVM?

I am creating a modular desktop application using Prism 5 with MEF, WPF 4.5 and the MVVM pattern. 我正在使用带有MEF,WPF 4.5和MVVM模式的Prism 5创建一个模块化桌面应用程序。

The application shell has a Ribbon control containing an application menu, a home tab displaying a drop-down list of available modules, a contextual tab group control intended to be activated when working with data displayed by views containing a DataGrid (ContextualTabGroupHeader="Grid Tools"). 该应用程序外壳具有一个包含应用程序菜单的功能区控件,一个显示可用模块下拉列表的主页选项卡,一个上下文选项卡组控件,该控件旨在在处理包含DataGrid的视图显示的数据时激活(ContextualTabGroupHeader =“ Grid Tools “)。 The Ribbon control also has a Prism Region (RibbonRegion) into which modules can add their own tab groups. 功能区控件还具有一个“棱镜区域”(RibbonRegion),模块可以在其中添加自己的选项卡组。 The only other control in the shell is a Content control defining another Prism Region (MainRegion). 外壳中的唯一其他控件是定义另一个棱镜区域(MainRegion)的Content控件。

When a module is selected it can optionally provide views for loading into the RibbonRegion and/or MainRegion areas of the shell. 选择模块后,可以选择提供视图以加载到外壳的RibbonRegion和/或MainRegion区域。 I use MEF [ExportMetaData] attributes read during aggregate catalogue configuration to manage this, for example: 我使用在聚合目录配置期间读取的MEF [ExportMetaData]属性来管理此属性,例如:

[ExportMetadata("ModuleDisplayName", "Example #1")]
[ExportMetadata("MainMenuView", "ExampleModule.Views.ExampleMenuView")]
[ExportMetadata("MainView", "ExampleModule.Views.ExampleTabsView")]

The module view loaded into the MainRegion could be anything capable of being added to a content control – it could, for instance, be a User Control containing a Tab control defining another module specific Prism Region into which the module could load one or more tabs containing views in response to the module ribbon tab commands. 加载到MainRegion中的模块视图可以是能够添加到内容控件中的任何内容–例如,可以是包含Tab标签控件的用户控件,该Tab标签控件定义了另一个模块特定的Prism Region,模块可以在其中加载一个或多个包含以下内容的标签视图以响应模块功能区选项卡命令。 The majority of modules will have 1 or more views that can display data using a DataGrid control and I want to make a number of tools (eg tagging, sorting, filtering, calculating etc.) commonly available to DataGrids contained in a view that has a view model datacontext that implements a particular interface for responding to the contextual tab commands (eg IGridTools). 大部分模块将具有1个或多个视图,这些视图可以使用DataGrid控件显示数据,而我想提供视图中包含的DataGrid常用的许多工具(例如,标记,排序,过滤,计算等)。查看实现了用于响应上下文选项卡命令的特定接口的模型数据上下文(例如IGridTools)。

The problem: So far, all of this is working quite well: I can activate different modules and navigate views and display data in DataGrid controls – but now I have run into my problem: How do I make the 'Grid Tools' contextual tab visible/hidden in response to a DataGrid control becoming available/unavailable (assuming the inherited datacontext or view model implements the required interface of course)? 问题:到目前为止,所有这些工作都很好:我可以激活不同的模块并导航视图并在DataGrid控件中显示数据-但现在我遇到了问题:如何使“网格工具”上下文选项卡可见/隐藏以响应DataGrid控件变得可用/不可用(假设继承的datacontext或view模型实现了所需的接口)?

I can detect when a view is navigated to/from but in the case of a module using a tab control to show multiple views (which may or may not all contain a datagrid) I would also need to know when the selected tab has the requisites to display the Ribbon contextual tab. 我可以检测视图何时导航到/从中导航,但是在使用选项卡控件显示多个视图的模块的情况下(该视图可能或可能不都包含一个数据网格),我还需要知道选定的选项卡何时具有必要条件显示功能区上下文选项卡。 I have seen examples of using IActiveAware but generally people are happy to use the Focus/LostFocus events to change the IsActive Boolean property. 我已经看到了使用IActiveAware的示例,但是通常人们很乐意使用Focus / LostFocus事件来更改IsActive布尔属性。 The trouble is that the view containing the DataGrid may or may not have other controls that could possibly take focus (as could the main Ribbon control) but if the DataGrid is still visible I would like the contextual grid tools ribbon tab to also remain visible. 问题在于包含DataGrid的视图可能具有或可能不具有其他可能引起焦点的控件(如主Ribbon控件一样),但是如果DataGrid仍然可见,我希望上下文网格工具的Ribbon Ribbon选项卡也保持可见。

One possible solution would be the following: 一种可能的解决方案如下:

Considering that the Region is defined on a ContentControl , you could ask the Region for the ActiveViews , which in this case there would be only one, that is, the only Active View that is being shown. 考虑到Region是在ContentControl上定义的,您可以向Region询问ActiveViews ,在这种情况下,只有一个,即唯一显示的Active View

Therefore, you would not need the Focus/LostFocus events. 因此,您不需要Focus / LostFocus事件。 The Region would take care of updating its ActiveViews list each time you navigate to a different Tab or View . 每当您导航到其他TabView时Region都会负责更新其ActiveViews列表。

Finally, in order to decide whether to show or hide the GridTools, you should evaluate if the Region's ActiveView contains a Grid control or not. 最后,为了确定是显示还是隐藏GridTools,您应该评估Region的ActiveView是否包含Grid控件。 You could achieve this by looking at UserControl's Content property. 您可以通过查看UserControl的Content属性来实现。

I hope this helped you, Regards. 希望这对您有所帮助,Regards。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM